Francesco
Francesco

Reputation: 397

Joomla: display page heading of menu item

I want to display the page heading of a Joomla menu item if this is filled. 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?

Upvotes: 2

Views: 750

Answers (2)

TVBZ
TVBZ

Reputation: 594

I had similar need and noticed this code doesn't work in Joomla 4. Here is my solution:

Joomla 3

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

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.

Upvotes: 0

Francesco
Francesco

Reputation: 397

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>

Upvotes: 1

Related Questions