php - 生成与 h1 标签完全相同的元页面标题

标签 php html joomla

基于 Joomla 的网站。我有很多页面,其中 h1 header 被提及为产品详细信息,并通过 PHP 根据产品详细信息显示。有 2 个文件:default.phpview.html.php

默认.php :

<h1>Used <?php echo $this->CatName; ?> <?php echo $this->prodDet->prod_name;?> Toy for Sale </h1>

这会正确显示 h1 标签。我想生成页面的元标题并使用在 view.html.php 中生成的 h1 输出。这一行定义了页面的标题:

$this->document->setTitle($title);

这一行定义了标题 h1 :

"{$this->item->heading}";

完整代码:

protected function _prepareDocument()
{
  $app = JFactory::getApplication();
  $menus = $app->getMenu();
  $title = null;

  // Because the application sets a default page title,
  // We need to get it from the menu item itself
  $menu = $menus->getActive();

  if ($menu)
  {
    $this->params->def('page_heading', $this->params->get('page_title', $menu->title));
  }
  else
  {
    $this->params->def('page_heading', JText::_('COM_USEDCAR_DEFAULT_PAGE_TITLE'));
  }

  $title = $this->params->get('page_title', '');

  if (empty($title))
  {
    $title = $app->get('sitename');
  }
  elseif ($app->get('sitename_pagetitles', 0) == 1)
  {
    $title = JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
  }
    elseif ($app->get('sitename_pagetitles', 0) == 2)
  {
    $title = JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
  }

  $title = "{$this->item->heading}";
  $this->document->setTitle($title);

  if ($this->params->get('menu-meta_description'))
  {
    $this->document->setDescription($this->params->get('menu-meta_description'));
  }

  if ($this->params->get('menu-meta_keywords'))
  {
    $this->document->setMetadata('keywords', $this->params->get('menu-meta_keywords'));
  }

  if ($this->params->get('robots'))
  {
     $this->document->setMetadata('robots', $this->params->get('robots'));
  }
}

title 标签中的输出是heading。如何将此 h1 标签输出而不是 $title

最佳答案

这是代码的标题部分的作用:

// getting title from params
  $title = $this->params->get('page_title', '');

// trying to get it right
  if (empty($title))
  {
    $title = $app->get('sitename');
  }
  elseif ($app->get('sitename_pagetitles', 0) == 1)
  {
    $title = JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
  }
    elseif ($app->get('sitename_pagetitles', 0) == 2)
  {
    $title = JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
  }

// overwrite everything above with some value, making above code useless
  $title = "{$this->item->heading}";
  $this->document->setTitle($title);

我可能是错的,但如果我没记错的话,如果值不存在,它将在转换为字符串时返回变量名。这里的“标题”可能是空的。

您可能希望将代码更改为如下内容:

[...]

if(!title){
  if(property_exists($this, 'item') && property_exists($this->item, 'heading') && $this->item->heading){
    $title = $this->item->heading;
  } else {
    $title = sprintf('Used %s %s Toy for Sale' , $this->CatName, $this->prodDet->prod_name);
  }
}
$this->document->setTitle($title);

您不妨将标题保存到 session 中并在任何地方重复使用:

[...]
$this->document->setTitle($title);

// save title to session
$_SESSION['page_title'] = $title;

并更新之前的循环:

// getting title from params
  $title = (isset($_SESSION['page_title']) && $_SESSION['page_title'])? $_SESSION['page_title'] : $this->params->get('page_title', '');

if (empty($title)){
[...]

完整代码应该是这样的:

[...]
session_id() || session_start();

$title = (isset($_SESSION['page_title']) && $_SESSION['page_title'])? $_SESSION['page_title'] : $this->params->get('page_title', '');

if(!title){
  if(property_exists($this, 'item') && property_exists($this->item, 'heading') && $this->item->heading){
    $title = $this->item->heading;
  } else {
    $title = sprintf('Used %s %s Toy for Sale' , $this->CatName, $this->prodDet->prod_name);
  }
}

if (empty($title))
{
  $title = $app->get('sitename');
}
elseif ($app->get('sitename_pagetitles', 0) == 1)
{
  $title = JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
}
  elseif ($app->get('sitename_pagetitles', 0) == 2)
{
  $title = JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
}

$_SESSION['page_title'] = $title;
$this->document->setTitle($title);
[...]

如果你愿意,你也可以放弃一切,像那样走:

[...]
$title = $this->params->get('page_title', '');

if(!title){
  if(property_exists($this, 'item') && property_exists($this->item, 'heading') && $this->item->heading) {
    $title = $this->item->heading;
  } elseif(
        property_exists($this, 'CatName') && 
        property_exists($this, 'prodDet') && 
        property_exists($$this->prodDet, 'prod_name') && 
        $this->CatName && 
        $this->prodDet->prod_name
      ){
    $title = sprintf('Used %s %s Toy for Sale' , $this->CatName, $this->prodDet->prod_name);
  } else {
    $title = $app->get('sitename');
  }
}

$this->document->setTitle($title);
[...]

代码未经测试,但它应该让您走上正确的轨道:)

关于php - 生成与 h1 标签完全相同的元页面标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45385512/

相关文章:

PHP 正则表达式仅提取不同字符串的部分

php - CodeIgniter从涉及mysql数据库的数据表中删除行

php - 如何在电子邮件内容中使用 <br> 标签?

php - joomla fatal error : Allowed memory size of 272629760 bytes exhausted

css - 强制 rowspan 不拆分新页面中的其他列数据

javascript - 重新编码函数

html - 如何更改 Material-UI 中的文本字段焦点样式?

html - 在跨度内右对齐数字的最简单方法。想不通

javascript - IE 中的 HTML CSS 下拉图像 - 下拉图标看起来在文本框之外并且有空格。

joomla - 在 Joomla Controller 中注册任务