【问题标题】:Generate meta page title exactly as on h1 tag生成与 h1 标签完全相同的元页面标题
【发布时间】:2018-01-05 05:35:19
【问题描述】:

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

default.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'));
  }
}

标题标签中的输出是heading。如何把这个h1标签输出而不是$title

【问题讨论】:

  • 在线上方:$this-&gt;document-&gt;setTitle($title); 您可以将$title 重新定义为您想要的任何内容。只要确保您没有破坏该函数的一般逻辑即可。
  • @Difster - 查看已更新,未显示正确的标题标签,任何帮助
  • 这是 joomla 吗?如果是,您可以在菜单选项中设置页面标题
  • 是的,这是 joomla,但它是一个自定义组件,并且菜单中没有这样的页面标题,因为此菜单中有 100 多个产品页面,并且需要这些页面的页面标题类似于 h1 标签在页面上创建。
  • 有谁能帮忙

标签: php html joomla


【解决方案1】:

以下是代码标题部分的作用:

// 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);

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

[...]
$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);
[...]

代码未经测试,但它应该让你走上正轨:)

【讨论】:

    【解决方案2】:

    您为什么不将h1 内容作为GET 参数发送到您的php 文档,然后在title 标记内使用echo 输出它?除非您避免动态回显,否则这可能是将文本输出为title 的一个很好的解决方案。

    【讨论】:

      【解决方案3】:

      我会将构造标题/标题的逻辑抽象为某个函数,然后使用该函数在两个地方构造标题。

      function constructTitle($catName, $prodName) {
          return "Used {$catName} {$prodName} Toy for Sale";
      }
      
      ...
      
      [in default.php]
      <h1><?php echo constructTitle($this->CatName, $this->prodDet->prod_name); ?></h1>
      
      [in view.html.php]
      $this->document->setTitle(constructTitle(..., ...));
      

      这使您可以在多个地方使用标题时用一个点来格式化标题。

      显然,该功能需要放置在这样的位置,以便在两个地方都可以访问它,并且您需要有一些方法来获取 类别名称产品名称 em> 在view.html.php。我对 joomla 不够熟悉,不知道这些。

      编辑: 澄清一下,没有真正的方法可以从 default.php 中“提取”标题,因为它是动态的。您需要处理 php 文件,然后也许您可以做一些正则表达式魔术,但这绝不是问题的正确解决方案。

      【讨论】:

        【解决方案4】:

        您可以将您的 h1 内容作为 GET 参数发送到您的 php-document,然后使用标题标签中的 echo 输出它吗?除非你避免动态回显,否则它会起作用。

        【讨论】:

          猜你喜欢
          • 2012-12-28
          • 2011-05-04
          • 2020-04-21
          • 2018-09-08
          • 1970-01-01
          • 1970-01-01
          • 2023-04-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多