douhandie6615 2017-08-28 18:37
浏览 41
已采纳

从其ID获取菜单项标题

I need to get the title of my top-level navigation. I already know the ID of the top-level menu-item but i need to get its title. How can one do that?

I currently have menu like this:

  • Level1
    • Level2 #1
    • Level2 #2
    • Level2 #3

If im on any of the level 2 pages i have the Level 1 menu-item ID. I need to get the title in the current theme-location instance.

   <?php if(getTopSelectedMenu('primary')):
            $topParent = intval(getTopSelectedMenu('primary'));
            $topParentTitle = "";
    ?>
        <div class="sidebar-menu-headline"><?=$topParentTitle?><i class="fa fa-arrow-down" aria-hidden="true"></i></div>
    <?php

        $args = array(
            'menu' => '',
            'menu_class' => 'sidebar-menu',
            'container' => 'ul',
            'theme_location' => 'primary',
            'level' => 2,
            'child_of' => $topParent,
            'echo' => FALSE,
            'fallback_cb' => '__return_false'
        );
        $submenu = wp_nav_menu( $args );

        if ( ! empty ($submenu) )echo $submenu;

    ?>
  • 写回答

1条回答 默认 最新

  • duanduo0520 2017-08-28 18:53
    关注

    use the below function to get menu title by passing menu id.

    Function: wp_get_nav_menu_object($menu) // $menu can be 'id','name' or 'slug'
    Returns : Object (
           term_id => 4
           name => My Menu Name
           slug => my-menu-name
           term_group => 0
           term_taxonomy_id => 4
           taxonomy => nav_menu
           description => 
           parent => 0
           count => 6
       )
    
    // in your case.
    $menu = wp_get_nav_menu_object($topParent );
    $menu_title = $menu->name;
    

    You can find more information here. https://codex.wordpress.org/Function_Reference/wp_get_nav_menu_object

    To get the Menu title, Use the below code

    if(getTopSelectedMenu('primary')){
        $topParentId = intval(getTopSelectedMenu('primary'));
        $menuLocations = get_nav_menu_locations(); // Get our nav locations (set in our theme, usually functions.php)
                                               // This returns an array of menu locations ([LOCATION_NAME] = MENU_ID);
    
        $menu =  wp_get_nav_menu_object($menuLocations['primary']) ; // Get the *primary* menu object
    
        $primaryNav = wp_get_nav_menu_items($menu->term_id); // Get the array of wp objects, the nav items for our queried location.
    
        foreach ( $primaryNav as $navItem ) {
            if ($navItem->ID == $topParentId ) {
                $topParentTitle = $navItem->title;
            }
        }
    }
    <div class="sidebar-menu-headline"><?=$topParentTitle?><i class="fa fa-arrow-down" aria-hidden="true"></i></div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统