web-dev-qa-db-ja.com

アクティブなメニュー項目のエイリアスまたはタイトルを取得するにはどうすればよいですか?

私のテンプレートでは、メニュー項目のエイリアスの名前を取得する追加のクラスをドキュメントの本文に追加したいので、ページ間で個別のスタイルを作成できます。

しかし、どうすればアクティブなメニュー項目のエイリアスまたはタイトルを取得できますか?

6
FFrewin

アクティブな(現在の)メニュー項目のデータを取得するには、次のコードを使用します。

_ $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
_

_$active_は、現在のメニュー項目のオブジェクトです。

print_r($active)を使用して出力すると、そのすべてのプロパティを表示できます。

上記の出力例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
        )

)
_

プロパティのいずれかを使用するには、_$active->'property name';_をエコーできます。

例えば_echo $active->alias;_

10
FFrewin