doufendi9063 2014-01-29 03:29
浏览 29

获取wordpress中的菜单项数量

Is it possible to get only the 1st three items in wordpress menu?

for example I have these menus.

HOME ABOUT PROPERTY SEARCH CONCIERGE CONTACT

and I want to get ony the first 3 menus.

your help is much appreciated.

  • 写回答

1条回答 默认 最新

  • doulan4371 2014-01-29 03:36
    关注

    I dont think there is any argument for that but this can be done like this:

       $menu_name = 'custom_menu_slug'; // Get the nav menu based on $menu_name (same as     'theme_location' or 'menu' arg to wp_nav_menu)
    
        if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
        $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
    
        $menu_items = wp_get_nav_menu_items($menu->term_id);
        $count = 0;
    
        foreach ( (array) $menu_items as $key => $menu_item ) {
          $count++;
          if($count == 3 ) {
            break;
           }
          $title = $menu_item->title;
         }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?