简体   繁体   English

Joomla:显示菜单项的页面标题

[英]Joomla: display page heading of menu item

I want to display the page heading of a Joomla menu item if this is filled.如果已填充,我想显示 Joomla 菜单项的页面标题。 I've tryed with this code wothout success:我试过这段代码没有成功:

<h1 class="title">
    <?php
        if (null === ($this->params->get('page_heading')))
        {
            $mydoc = JFactory::getDocument();
            $mytitle = $mydoc->getTitle();
            echo $mytitle;
        }
        else 
        {
            $active = JFactory::getApplication()->getMenu()->getActive();
            echo $active->params->get('page_heading');
        }
    ?>
</h1>

Any suggestion?有什么建议吗?

I've solved myself this way: 我已经这样解决了自己:

<h1 class="title">
    <?php $menu  = JFactory::getApplication()->getMenu();
        $active = $menu->getActive();
        $mydoc = JFactory::getDocument();
        $mytitle = $mydoc->getTitle();
        $pageHeading = $active->params->get('page_heading');
        if($pageHeading != "")
        {
            echo $pageHeading;
        }
        else 
        {
            echo $mytitle;
        }
    ?>
</h1>

I had similar need and noticed this code doesn't work in Joomla 4. Here is my solution:我有类似的需求,并注意到此代码在 Joomla 4 中不起作用。这是我的解决方案:

Joomla 3 Joomla 3

$menu = JFactory::getApplication()->getMenu()->getActive();
$title = $menu->title;
$page_heading = $menu->params->get('page_heading');
echo $page_heading ?? $title;

Joomla 4 Joomla 4

$menu = JFactory::getApplication()->getMenu()->getActive();
$title = $menu->title;
$page_heading = $menu->getParams()->get('page_heading');
echo $page_heading ?? $title;

The only difference between these 2 is how you get the menu item params, params in J3 vs getParams() in J4.这两者之间的唯一区别是如何获取菜单项params ,J3 中的参数与 J4 中的getParams()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM