0

I have made a menu in the admin page ordered like this:

parent
    - child 1
    - child 2
    - child 3
parent
    - child 1
    - child 2
    - child 3
parent
    - child 1
    - child 2
    - child 3

When I load them in my navbar with the following code it works just fine:

wp_nav_menu( array(
    'theme_location' => 'menu-1',
    'menu_id'        => 'primary-menu',
    'menu_class'     => 'top-menu',
    ) );

However, I want to load a specific menu on a page based on it's parent. I have done this as followed:

<?php 
$children = get_posts( array( 'post_parent' => $parent, 'post_type' => 'page', 'numberposts' => 9, 'orderby' => 'menu_order', 'order' => 'ASC')); 
?>

But when it is displayed it displays the menu according to their publishing date:

- child 2
- child 1
- child 3

I know I can just change the publishing date but this may conflict in the future.

How do I order these children the same way as I have in the navbar?

1

menu_order doesn't mean the order in which it is used in any given custom menu. It means order by the "Order" attribute as entered in this box:

enter image description here

Counterintuitively this attribute is saved into the menu_order column in the wp_posts table in the database, which is why the orderby argument value is menu_order.

I'm not certain as I wasn't using WordPress at the time, but I believe this is a holdover from a time when WordPress did not have customisable menus and the only way to output a menu of pages was the wp_list_pages() function, which was introduced in 1.5.0 (wp_nav_menu() wasn't introduced until 3.0.0). When that was the way of doing menus ordering menu items was done with this attribute.

  • Okay that makes sense, thank you! I really want to avoid ordering them by entering an order number. Is there a way to order it exactly as the menu structure? – Rubenxfd Mar 23 '18 at 12:01
  • No, not really. There are plugins available that let you order pages with drag and drop, if that makes it easier. – Jacob Peattie Mar 23 '18 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.