2

As I'm making one page website. Here is question I asked earlier:

Calling Specific Pages with wp query Part II

As I'm calling all pages by WP_Query which is "order by = ASC" I want to call it "order by menu structure" not menu_order.

For E.g: screenshot

Now I want home to the top then services then testimonials. depend on menu structure..

How can I achieve it.. Please Help .. Thanks in advance!

3
  • Could you provide a basic example of what you would consider "menu structure"? For example menus can be nested, what would be the order in such cases?
    – Rarst
    Mar 26, 2015 at 20:45
  • @rarst I updated my question. Waiting for your answer. Thanks!
    – Hussy571
    Mar 26, 2015 at 21:06
  • Hussy I don't believe there is a method available to query by the Appearance > Menu structure.
    – jdm2112
    Mar 26, 2015 at 21:19

2 Answers 2

3

You may be able to use a WP_Query on nav_menu_item since it is its own post type. I've never done this but maybe it would work like you need it to, worth a shot. There are three other possibilities:

Option 1 - Get Your Nav Menu Items

There's a functions called wp_get_nav_menu_items() which will return you an array of your menu items that you can then loop through and display. Here's an example of how you could use it. There are a few "Gotcha!" here:

  • $item->ID is the current navigational items ID not the post_id. The post ID is now $item->object_id.
  • $item->title is the current navigational items title and not necessarily the post_title though by default it is, it can still be modified and changed. The most reliable way to get the post title is to use the object_id and pass it into get_the_title() function.

$nav_items = wp_get_nav_menu_items( 'Main Menu', array(
    'order'                  => 'ASC',                  // List ASCending or DESCending
    'orderby'                => 'title',                // Order by your usual, menu_order, post_title, etc. Check WP_Query
    'post_type'              => 'nav_menu_item',        // To be honest, I'm not sure why this is an option, leave it be.
    'post_status'            => 'publish',              // If there are private / draft posts in our menu, don't show them
    'output'                 => ARRAY_A,                // Return an Array of Objects
    'output_key'             => 'menu_order',           // Not sure what this does
    'nopaging'               => true,                   // Not sure what this does
    'update_post_term_cache' => false                   // Not sure what this does
) );

if( ! empty( $nav_items ) ) {
    foreach( $nav_items as $item ) {
        echo "{$item->title} - " . get_the_title( $item->object_id );
        echo "<br />\n";
    }
}

Option 2 - Custom Nav Walker

You could just display your menu as is using wp_nav_menu() and pass in a Custom Walker Function to modify it's output. An example of this could be automatically pulling that menu items child pages, without actually adding those pages to the physical menu. Child Walker Class

Option 3 - Page Menu Order

Usually when I create a website for a client I have a WordPress menu and I also mirror the admin panels page order with Page Attribute menu_order. This way you could query pages using WP_Query and orderby => 'menu_order'

Image Found on codeholic.in


Other than that, in short there is no easy orderby => 'My Menu', you'll have to find an alternative or a workaround.

5
  • Hi, McGee. Thanks for your answer really well explained .. I know about option 3 as I asked in question not menu_order. Well can u explain a little bit more about wp_get_nav_menu_items() with coding example or any link to tutorial. How can I use it.?? .. Thanks again..
    – Hussy571
    Mar 27, 2015 at 9:46
  • @Hussy571 I've added an example of how to use wp_get_nav_menu_items(). The first parameter passed will be whatever your menu is called. Unfortunately, I'm not entirely sure what all the parameters do, I tried messing around with them and got unexpected results. The best references to changes some of these parameters will be The Codex on wp_get_nav_menu_items() and get_posts().
    – Howdy_McGee
    Mar 27, 2015 at 19:13
  • I choose option 3 . couldn't make it with option 1 . :'( . I will post a news question on how to make it with wp_nav_menu_item() .. but again thanks for explanation.
    – Hussy571
    Mar 29, 2015 at 9:03
  • Option 1, how can I get the item link?
    – ValRob
    Oct 22, 2018 at 11:37
  • 1
    @ValRob The ID is held in $item->object_id so you can pass that to get_permalink() or whatever link function you need.
    – Howdy_McGee
    Oct 22, 2018 at 14:28
2

You could use the function wp_get_nav_menu_items() instead of wp_query() to retrieve the menu items from a particular menu in the menu structure order. Once you've done that, you could loop through the menu items one by one to do with as you wish (including filtering them down further on type or other info). This is assuming all pages are in the menu (which they would need to be if you're using that to understand the order).

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.