Let’s take a look at wp_get_nav_menu_items
code reference.
It takes two parameters:
$menu
– (int|string|WP_Term) (Required) Menu ID, slug, name, or object,$args
– (array) (Optional) Arguments to pass to get_posts().
So we can use get_posts
args in here… And if we want to get only top-level posts, then post_parent
arg comes useful…
So something like this should do the trick:
function cr_get_menu_items($menu_location)
{
$locations = get_nav_menu_locations();
$menu = get_term($locations[$menu_location], 'nav_menu');
return wp_get_nav_menu_items($menu->term_id, array('post_parent' => 0));
}
Related Posts:
- Remove Container Element From wp_nav_menu() Markup
- Breadcrumbs showing Parent and Child Pages
- Is there a way to add a class to non current menu item?
- Menu fallback “menu_class” rendering a “div” instead of a “ul”
- How do I create page navigation linking to each H2 within the page? [closed]
- Why does the ‘wp_nav_menu’ function work only until a menu is created?
- Custom Static Links For Specific Menu Right Before/Next The wp_nav_menu Function now working
- Categories as main menu items and subcategories as nested lists
- retrieves menu and converts it to a readable array
- Functions php shortcode for displaying main menu with no child items
- Nav menus all outputting the same links
- Secondary Menu and Logged In Users
- Default Nav Highlight
- Multiple navigation menu in same location
- wp_nav_menu work in functions.php but not in the theme
- How would I make a function to dynamically determine if user is logged to change navigation menu text?
- Bizarre Child Menu Issue on WP Site
- How to automatically add counted number to navigation menu items?
- How to add widget in main menu
- wp_nav_walker that interacts with widgets to setup mega menu
- Why don’t ‘wp_nav_menu’ CSS classes work until a menu is created?
- Redirect to another page using contact form 7? [closed]
- append to existing parent theme function
- ACF not showing
- How to customize the_archive_title()? Cannot figure out how to change the value it produces [duplicate]
- Control the srcset much more (not all sizes in it each time)
- Check if the image size is available and if not use ‘full’ image size
- Way to store featured image width and height in two separate variables?
- Remove Custuomize Sections
- Can we create our own theme’s function.php?
- Define PHP variable from a seperate API if statement
- How do I hook into the container of wp_nav_menu?
- Excerpt for pages not showing
- get_pages() Returns Only One Item
- Add login hyperlink to secondary navigation menu
- Some questions about how proper add 2 CSS file in a WordPress theme?
- New custom menu created. how to apply it?
- Dropline menus — seperators between children only?
- Filter nav menu items HTML tags and wrap inner text with span
- Does size of a theme’s functions.php file matter?
- get_next_posts_link() returns no result despite available posts
- Change menu based on page template via functions.php
- PHP 7.1 | Warning: A non-numeric value encountered in
- Show some menu sub items as dropdown under a menu item
- How to add custom li item to wordpress menu
- How to insert class in each list of categories?
- Add Login/Logout Menu Item to Primary Nav “My Account” Submenu [Woocommerce] [closed]
- I can’t add CSS with functions.php
- Getting the teaser text without overriding global variables
- Combine two active_callbacks into one on theme customizer
- how do i develop a global nav and local nav to be wordpress dynamic?
- why is this function firing on all child menu items as well?
- WP Customizer API loaded into functions.php
- call_user_func_array() expects parameter 1 to be a valid callback, function
- why can’t i add front.css to my frontpage.php
- How to modify mobile nav menu text in theme
- Different methods of adding menu support to custom theme
- “options.php” not found
- Adding a section in line at the end of the main navigation menu in php
- How to store a number (coming from cookie) into query vars for later usage in other filters?
- How to add aria role and schema markup to custom walker container
- Is this an acceptable practice for WordPress theme development?
- What is the correct way to include my new functions and scripts in WordPress?
- How to register a menu based on a ACF condition
- How to hightlight all ancestor menu items of a child page NOT displayed in menu
- Nav-Menu not showing up
- Rewrite Question Mark in Post Template with URL Parameter for clean URLs
- contact form in template with jquery,validate and ajax
- current_page_item is missing inside wp_nav_menu
- search form leads to 404
- Why a SlideShow (made using JQuery FlexSlider) can’t work if I load it form functions.php but work if I load it from my footer.php file? [closed]
- Why I can’t load my JavaScripts using an hook inside my functions.php file?
- Unable to add extra elements to the main navi (wp_nav_menu_items)
- How Can I add a menu to the theme from withen the function.php file
- define css class in functions.php
- Custom Menu in Admin doesn’t change menu in browser
- Remove the Tag from wp_nav_menu
- Custom nav menu not showing
- Conditional custom menu?
- wp_nav_menu displaying all pages
- Woocommerce Variable Product Dropdown for Custom Shop Template
- Warning at top of website & top menu gone
- How to display an image before title text in menu items
- How to fix Function create_function() is deprecated in / themes
- Exclude specific post from query
- Advanced method to control cache of enqueued style/script
- WordPress wp_dequeue_script remove all other scripts
- Header menu aligned right on all pages except for single-post page [closed]
- Programatically Created Menu not Editable in Dashboard
- Can I use ‘Featured Image’ as a hero image and a thumbnail?
- 3 Level Menu Navigation (3rd Level not displaying)
- Is_Page doesnt detect my page
- Test CDN link from function.php or wp_enqueue_script/style?
- How to customize archive widget only for cpt?
- wp_title() return random number
- Why does echo on functions.php not yield result in source code?
- How can I show a custom WordPress menu anywhere I want on my website?
- Combine multiple menus using the filter wp_nav_menu_items
- How to offset main query without affecting other queries and backend
- Menu and category highlighting for a single post