7

In my template I want to give an extra class to the body of the document that will take the names of the alias of the menu item, so I can create individual styling among the pages.

But how can I get the alias or title of the active menu item ?

1 Answer 1

12

To get the data of the active (current) menu item, use the following code:

 $app       = JFactory::getApplication(); // Access the Application Object

 $menu      = $app->getMenu(); // Load the JMenuSite Object

 $active    = $menu->getActive(); // Load the Active Menu Item as an stdClass Object

The $active is an object of the current menu item.

Outputting with print_r($active) you can have a view of all of its properties.

Example output of the above print_r();

stdClass Object
(
    [id] => 136
    [menutype] => main-menu
    [title] => Photos
    [alias] => photos
    [note] => 
    [route] => photos
    [link] => index.php?option=com_content&view=article&id=157
    [type] => component
    [level] => 1
    [language] => en-US
    [browserNav] => 0
    [access] => 1
    [params] => Joomla\Registry\Registry Object
        (
            [data:protected] => stdClass Object
                (
                    [show_title] => 
                    [link_titles] => 
                    [show_intro] => 
                    [info_block_position] => 
                    [show_category] => 
                    [link_category] => 
                    [show_parent_category] => 
                    [link_parent_category] => 
                    [show_author] => 
                    [link_author] => 
                    [show_create_date] => 
                    [show_modify_date] => 
                    [show_publish_date] => 
                    [show_item_navigation] => 
                    [show_vote] => 
                    [show_icons] => 
                    [show_print_icon] => 
                    [show_email_icon] => 
                    [show_hits] => 
                    [show_tags] => 
                    [show_noauth] => 
                    [urls_position] => 
                    [menu-anchor_title] => 
                    [menu-anchor_css] => 
                    [menu_image] => 
                    [menu_text] => 1
                    [page_title] => 
                    [show_page_heading] => 0
                    [page_heading] => 
                    [pageclass_sfx] => 
                    [menu-meta_description] => 
                    [menu-meta_keywords] => 
                    [robots] => 
                    [secure] => 0
                )

        )

    [home] => 0
    [img] => 
    [template_style_id] => 0
    [component_id] => 22
    [parent_id] => 1
    [component] => com_content
    [tree] => Array
        (
            [0] => 136
        )

    [query] => Array
        (
            [option] => com_content
            [view] => article
            [id] => 157
        )

)

To use any of the properties, you can echo $active->'property name';

e.g. echo $active->alias;

1
  • 2
    Just a shortened way to do this : $menualias = JFactory::getApplication()->getMenu()->getActive()->alias; then in body: id="aliasmenu-<?php echo $menualias; ?>"
    – ghazal
    Nov 13, 2014 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.