wp_list_pages按照顶部导航菜单顺序和wordpress的子项排序

时间:2016-09-30 08:37:24

标签: wordpress themes wordpress-theming

我试图在左侧边栏上显示一个框,其中包含当前页面子菜单的链接。但订单不适用于菜单!这个问题是什么?

<div class="sidebar-box">
                <div class="sidebar-box-title">
                    <h4><?php echo get_the_title($post->post_parent); ?></h4>
                </div>
                <ul class="links">
                    <?php wp_list_pages('sort_order=asc&title_li=&sort_column=menu_order&depth=1&child_of='.$post->post_parent); ?>
                </ul>
            </div> 

DEMO:http://www.testhosting.co.uk/speedshealthcare/healthcare-supplies/care-home-pharmacy/

侧边栏链接顺序必须与顶级菜单顺序相同

1 个答案:

答案 0 :(得分:0)

我发现了http://wpsmith.net/2011/how-to-get-all-the-children-of-a-specific-nav-menu-item/

并编写了这段代码,所以修好了!

function get_nav_menu_item_children($ menu,$ depth = true){

 $post_id = $post_id ? : get_the_ID();
$menu_items = wp_get_nav_menu_items($menu);

$parent_item_id = wp_filter_object_list($menu_items, array('object_id' => $post_id), 'and', 'menu_item_parent');

if (!empty($parent_item_id)) {
    $parent_item_id = array_shift($parent_item_id);
    $parent_post_id = wp_filter_object_list($menu_items, array('ID' => $parent_item_id), 'and', 'object_id');

    if (!empty($parent_post_id)) {
        $parent_post_id = array_shift($parent_post_id);

       $parent_id=  get_post($parent_post_id)->ID;
    }
}




$nav_menu_item_list = array();

foreach ((array) $menu_items as $nav_menu_item) {

    if ($nav_menu_item->post_parent == $parent_id) {

        $nav_menu_item_list[] = $nav_menu_item;

    }
}
return $nav_menu_item_list;

}

<强>用法

 $menuSide=get_nav_menu_item_children('Main menu');

                      foreach ($menuSide as $link):
                        $active='';
                          if(intval($post->ID)===intval($link->object_id)){
                              $active=' current_page_item ';

                          }
                        echo ' <li class="'.$active.'"><a  href="'.$link->url.'">' . $link->title . '</a></li>';
                    endforeach;