dsijovl015728613
2018-08-24 14:05
浏览 91
已采纳

如何获取当前页面菜单项祖先名称wordpress

I would like to know how to retrieve as variable the name of the current page. For exemple, My menu is like :

Item 1
Item 2 => sub-item 1

Item 1 & 2 are custom links. sub-item1 is my page.

When I'm on this page, I want to retrieve "Item 2" name (to build my custom breadcumbs)

Thanks

图片转代码服务由CSDN问答提供 功能建议

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

  Item 1 
Item 2 => 子项目1 
   
 
 

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

当我在这个页面上时,我想要检索“Item 2”名称(以构建我的自定义breadcumbs)

谢谢

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dqvs45976 2018-08-27 13:23
    已采纳

    BREADCRUMBS:

    Here is a breadcrumbs function that work with the hierarchy of your menu. Add this to your theme's '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);
         }
    }
    

    You can then call as <?php my_menu_breadcrumb('header-menu'); ?> where 'header-menu' is the menu location name. In the loop, $menu_item->title will return page title and $menu_item->url it's URL.

    PARENT MENU TITLE:

    Here is the function to get parent menu item title(s) of current page - add this to your theme's '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;
         }
    }
    

    You can then call as below where 'header-menu' is the menu location name.

    $parentitems = my_menu_parent( 'header-menu' );
    foreach ( $parentitems as $parentitem ) {
        echo $parentitem."<br>";
    }
    
    已采纳该答案
    打赏 评论
  • dongzhiyan5693 2018-08-27 10:04

    You can user this

    $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;  
    }
    
    打赏 评论
  • douwan7382 2018-08-27 12:25

    There are plenty of tutorials and code snippets online describing how to show the active page’s children (or if on one of those children, the active page’s siblings) as a sub-menu in WordPress. Usually something like this:

    <?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>";
    }
    ?>
    

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

    打赏 评论

相关推荐 更多相似问题