joomla 3.x - 如何在不使用 <jdoc:include type="head" /> 的情况下将 "metadescription" 和 "title" 包含在 header 中

joomla 3.x - how to include "metadescription" and "title" in the header without using <jdoc:include type="head" />

我正在尝试更好地控制我的 Joomla 网站的 header;对于某些页面,我不需要 header 中的很多东西。我决定制作一个不使用 <jdoc:include type="head" /> 的模板,因为它加载了很多我不需要的东西。

搜索时,我发现了这个关于该主题的旧 post,并且在网络上有人在寻找相同的东西。 Manually control <head> markup in Joomla

我想知道是否可以将我的 index.php 模板文件添加到 PHP 代码中,这样就可以获得 Joomla 的 "metadescription" 和 "title"出版物。像这样:

  <?php defined( '_JEXEC' ) or die; ?>
    <!doctype html>
    <html lang="<?php echo $this->language; ?>"> 
    <head> 
    <meta name="viewport" content="width=device-width />
    <meta name="description" content="<?php echo **code metadescription** ?>" />
    <title><?php echo **code to get title** ?></title>
    </head>
    <body> 
    <jdoc:include type="component" />  
    </body>
    </html> 

只需使用PHPinclude()函数

top.php

<meta name="viewport" content="width=device-width />
<meta name="description" content="<?php echo **code meta description** ?>" />
<title><?php echo **code to get title** ?></title>

并且在您当前的文件中只包含文件(top.php),例如

<?php defined( '_JEXEC' ) or die; ?>
    <!doctype html>
    <html lang="<?php echo $this->language; ?>"> 
    <head> 
    <?php include("top.php"); ?>
    </head>
    <body> 
    <jdoc:include type="component" />  
    </body>
    </html> 

我不知道这是不是一个好方法,但是你可以按照以下样式取消所有css和js:

unset($doc->_styleSheets[$this->baseurl.'/path/to/some.css']);
unset($doc->_scripts[$this->baseurl.'/path/to/some.js']);

我建议不要删除 content-type 或 x-ua-compatible 等元数据。这些标签在某些浏览器中支持您的网站。网站图标 link 对书签很有帮助。

很好,过了一会儿我找到了我正在寻找的代码,也许它可以帮助其他人,它对我有用......在我添加的模板的 index.php 文件中:

<?php defined( '_JEXEC' ) or die;
$doc =JFactory::getDocument(); 
$meta_description = $doc->getMetaData("description"); 
$title = $doc->getTitle();
?>
<!doctype html>
<html lang="<?php echo $this->language; ?>">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /> 
<meta name="description" content="<?php echo "$meta_description"; ?>" />
<title><?php echo "$title" ?></title> 
</head>
<body> <jdoc:include type="component" />  </body>
</html>