最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp list pages - wp_list_pages sort order by top nav menu order and child of

programmeradmin3浏览0评论

Im trying to show a box on left sidebar with links of current page's sub menus. but order is not applying same as menu! whats the problem?

<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: /

Im trying to show a box on left sidebar with links of current page's sub menus. but order is not applying same as menu! whats the problem?

<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/

Share Improve this question asked Sep 30, 2016 at 8:33 AminoAmino 3232 gold badges5 silver badges17 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

Because you are ordering only by menu_order, rather than menu_order post_title. In fact, you can just get rid of your sort_* arguments as wp_list_pages will output the correct natural order by default.

I found this http://wpsmith.net/2011/how-to-get-all-the-children-of-a-specific-nav-menu-item/

and wrote this code, so it fixed!

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;

}

Usage

 $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;

You should use the page editor and set the 'order'. Setting the page order is what the problem here is. Once you set the order (lowest to highest) it will display in wp_list_pages in that order and not the default order which is page_title as @TheDeadMedic has stated. By default wp_list_pages lists pages alphabetically by their post_title. So if you want anything different from that, you are going to have to go to your pages in the dashboard one-by-one, and set those orders. After that, you will be all set.

Hope that helps!

发布评论

评论列表(0)

  1. 暂无评论