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?
- Issues with title-tag and document_title_parts
- Mega Menu Walker
- Get menu object from theme_location
- Displaying Logged-In User Name in WordPress Menu
- How to get woocommerce inventory status [closed]
- How to add menu support to a theme?
- Organize functions.php
- How to override functions.php in child theme?
- How to activate “Description” metabox for menu item programmatically?
- Dynamically add sub-categories to any category in the menu
- Change parent theme file function in child themes functions.php
- Breadcrumb how i can display page title with parent > child title with permalink ? any Idea
- automatically adding submenu items
- Extend walker – navigation, adding data attribute to a tag
- Unregister Nav Menu with fallback?
- How to programmatically set a menu to be the Primary Menu?
- how to change WordPress menu markup/classes
- Dequeue script in template isn’t working
- Setting a Default ‘Theme Location’ When Creating a Menu
- Changing itemprop from logo to image on custom logo?
- Make menu structure match page heirarchy on page parent change
- Show excerpt for only first post in query
- How to add css class to image attached in all the posts?
- How to call custom function from functions.php in site-wide template files?
- How to enable template page only for a post id page
- My Admin bar covers my sticky navbar [closed]
- Multiple WordPress Menus that will only display all pages
- Hide Hamburger Menu On Specific Page (Front Page/Home Page)
- WordPress is adding margins and padding to my custom menu?
- Add caption functionality to custom WordPress theme
- How to add nav menu items to a specific position within a menu
- Using a page as home, how do I prevent nav from setting both home and page links active?
- Best practices regarding the creation of custom widgets?
- Custom Nav Walker sub-menu
- enqueue styles for only mobile wp
- Replace a menu with widget or a custom template file programmatically
- Removing specific menu items?
- Redirect to another page using contact form 7? [closed]
- Remove Custuomize Sections
- get_pages() Returns Only One Item
- New custom menu created. how to apply it?
- Add Login/Logout Menu Item to Primary Nav “My Account” Submenu [Woocommerce] [closed]
- Getting the teaser text without overriding global variables
- why is this function firing on all child menu items as well?
- How to modify mobile nav menu text in theme
- “options.php” not found
- How to add aria role and schema markup to custom walker container
- contact form in template with jquery,validate and ajax
- current_page_item is missing inside wp_nav_menu
- 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)
- Remove the Tag from wp_nav_menu
- Conditional custom menu?
- Woocommerce Variable Product Dropdown for Custom Shop Template
- Exclude specific post from query
- Advanced method to control cache of enqueued style/script
- Header menu aligned right on all pages except for single-post page [closed]
- 3 Level Menu Navigation (3rd Level not displaying)
- Why does echo on functions.php not yield result in source code?
- Menu and category highlighting for a single post
- WordPress Toggle Menu has stopped working
- Remove External Links from WordPress posts Using add_filter() in Theme functions.php
- How to retrieve image IDs from shortcode content?
- Need help with adding custom wordpress menu and sub-menu
- Class::$template_dir_path=get_template_directory();
- Language Switch Function
- Fallback dosen’t work when not using ‘menu’ attribute
- Language switcher for subdomains
- How can I add recent posts to menu like mashable
- How to add next height number in Order Attributes inside the Add new page. [duplicate]
- How to add drop symbol to parent nav items which have child
- WordPress 3.9 two menus in same position?
- Allow tags and attributes in post and pages content
- How to add a class name to the ancestor of a post?
- Undefined offset: 2
- How to make navigation a list without a plugin? [duplicate]
- Add custom css to theme
- How to add more than one menu for logged in members?
- Setcookie not setting cookies anywhere except admin