【问题标题】:How to get current page menu item ancestor name wordpress如何获取当前页面菜单项祖先名称wordpress
【发布时间】:2018-08-28 20:20:18
【问题描述】:

我想知道如何将当前页面的名称作为变量检索。 例如,我的菜单是这样的:

Item 1
Item 2 => sub-item 1

第 1 项和第 2 项是自定义链接。 sub-item1 是我的页面。

当我在这个页面上时,我想检索“项目 2”的名称(以构建我的自定义面包屑)

谢谢

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    面包屑:

    这是一个适用于菜单层次结构的面包屑功能。将此添加到主题的“functions.php”中。

    function my_menu_breadcrumb($theme_location, $separator = ' > ') {
        $locations = get_nav_menu_locations();
        if ( isset( $locations[ $theme_location ] ) ) {
            $menu = wp_get_nav_menu_object( $locations[ $theme_location ] );
            $menu_items = wp_get_nav_menu_items($menu->term_id);
            _wp_menu_item_classes_by_context( $menu_items );
            $breadcrumbs = array();
    
            foreach ( $menu_items as $menu_item ) {         
                if ($menu_item->current) {
                    $breadcrumbs[] = "<span title=\"{$menu_item->title}\">{$menu_item->title}</span>";
                }
                else if ($menu_item->current_item_ancestor) {
                    $breadcrumbs[] = "<a href=\"{$menu_item->url}\" title=\"{$menu_item->title}\">{$menu_item->title}</a>";
                }
            }
    
            echo implode($separator, $breadcrumbs);
         }
    }
    

    然后您可以调用&lt;?php my_menu_breadcrumb('header-menu'); ?&gt;,其中“header-menu”是菜单位置名称。在循环中,$menu_item-&gt;title 将返回页面标题和$menu_item-&gt;url 它的 URL。

    父菜单标题:

    这是获取当前页面父菜单项标题的功能 - 将其添加到主题的“functions.php”中。

    function my_menu_parent($theme_location) {
        $locations = get_nav_menu_locations();
        if ( isset( $locations[ $theme_location ] ) ) {
            $menu = wp_get_nav_menu_object( $locations[ $theme_location ] );
            $menu_items = wp_get_nav_menu_items($menu->term_id);
            _wp_menu_item_classes_by_context( $menu_items );
            $breadcrumbs = array();
    
            foreach ( $menu_items as $menu_item ) {         
                if ($menu_item->current_item_ancestor) {
                    $breadcrumbs[] = $menu_item->title;
                }
            }
    
            return $breadcrumbs;
         }
    }
    

    然后您可以如下调用,其中“header-menu”是菜单位置名称。

    $parentitems = my_menu_parent( 'header-menu' );
    foreach ( $parentitems as $parentitem ) {
        echo $parentitem."<br>";
    }
    

    【讨论】:

    • 嗨@Outsource Wordpress,感谢您的回答,比我的更好的功能!请给我一个只得到父母名字的方法
    • 漂亮!!非常感谢
    【解决方案2】:

    你可以使用这个

    $pagename = get_query_var('pagename');  
    if ( !$pagename && $id > 0 ) {  
    // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object  
    $post = $wp_query->get_queried_object();  
    $pagename = $post->post_name;  
    }
    

    【讨论】:

    • 嗨,我不需要页面名称...我需要页面的菜单父名称!
    【解决方案3】:

    网上有很多教程和代码 sn-ps 描述了如何在 WordPress 中将活动页面的子项(或者如果在其中一个子项上,活动页面的兄弟姐妹)显示为子菜单。通常是这样的:

    <?php
    $ref_post = empty($post->post_parent) ? $post->ID : $post->post_parent;
    $children = wp_list_pages('title_li=&child_of='.$ref_post.'&echo=0');
    if ($children) {
    echo "<ul>$children</ul>";
    }
    ?>
    

    参考:-https://www.minddevelopmentanddesign.com/blog/showing-current-pages-parents-sub-menu-items-custom-nav-menu-wordpress/

    【讨论】:

      猜你喜欢
      • 2011-06-17
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-20
      • 2016-02-25
      • 1970-01-01
      相关资源
      最近更新 更多