Get WordPress menu items only (not the whole menu)

You might find yourself in the situation where you need to get the menu items for a particular WordPress menu without grabbing the whole menu markup (for example, the nav part) itself or maybe you just want get the menu items data to output in your loop elsewhere.

Render the menu items without the nav

In the first scenario, we can render the menu without the container (usually nav) by using wp_nav_menu() and setting container to be blank.

https://gist.github.com/nickdavis/aa2cf126384a3550940767688b8f8959

In this example we just need to know the slug for the menu location we’re targeting.

Get the menu items data and render in a custom loop

In the second scenario, we can grab all the menu items data by passing the menu’s slug (rather than the menu location slug) to wp_get_nav_menu_items() like the following example.

https://gist.github.com/nickdavis/9947bc968903549afc3950d80db796b7

(For this technique you might need to get a WordPress menu from a specific menu location in order to discover the correct menu slug).

Leave a Reply

Your email address will not be published. Required fields are marked *