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 badges3 Answers
Reset to default 1Because 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!