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

如何获取当前页面菜单项祖先名称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

  • 写回答

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>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀