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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题