wp_nav_menu
has the argument fallback_cb
, which is the function called if a menu doesn’t exist. This is set to wp_page_menu
by default, which is why you see a list of pages if the menu doesn’t exist. If you explicitly set that to false
, then nothing will be output if the menu doesn’t exist.
EDIT-
Given a menu name, you can load the menu object with wp_get_nav_menu_object
. This will tell you if it exists, what its ID is (to pass as menu
argument), and how many menu items it has.
$menu_name = get_post_meta( $post->ID, 'MenuName', true );
$menu = wp_get_nav_menu_object( $menu_name );
if( is_object( $menu ) ){
echo 'This menu exists!';
echo 'This menu has ' . $menu->count . ' menu items.';
echo 'This menu ID is ' . $menu->term_id . '.';
} else {
echo 'A menu with that name doesn\'t exist';
}
Related Posts:
- Adding wrapper elements in the_date() like in the_title()?
- Loop on a wordpress Page instead of content coming from the WP text editor
- Mega Menu Walker
- start_lvl Ignored in Custom walker_nav_menu
- Displaying Logged-In User Name in WordPress Menu
- Hide main div if wp_nav_menu is empty
- How to remove a metabox from menu editor page?
- Hide a menu-item and its submenus and display a ‘Log in’ link if the user is logged out
- The best way to customize “nav-menu-template.php” to add if the ‘link_before’ is “checkbox”
- How to add active class to custom menu using while loop and wp_list_pages
- Exclude pages with certain template from wp_list_pages
- WordPress menu deletes when trying to add a hook
- Logout and redirect using the WP Menu
- Easiest way to show total number of subpages
- Switching between custom templates in a post type of the admin menu
- Use template for posts with a particular category grandparent
- page template for attachement page?
- Add a Second Menu to a theme that only support 1 menu
- Custom excerpt function re-factoring
- Have CSS Class Added with `is_page()` WP function
- Custom Template 404 for specific custom post type
- Add itemprop Schema.org Markup to li Elements in wp_nav_menu
- Run a filter when a walker runs
- Menu — How to add “current-menu-grand-ancestor” css class
- Hide Hamburger Menu On Specific Page (Front Page/Home Page)
- Showing content from one page on another
- how could I load a different template part by page
- Automatically adding new post categories to menu
- Add data attribute to each li in menu
- Change title only in dynamic page
- Unique icons next to each WordPress menu item
- Child page menu in sidebar
- Populate editor with some content of a page with a page template
- Title Case WordPress Menu Items
- Secondary navigation menu on one page
- WordPress Quick Question . How to Get Parent Link in Submenu in My Code
- Detecting classes, adding widgets, and adding divs in with a Nav Walker
- How to change menu order item
- Is the “_s” on this `sprintf(__(‘Page %s’, ‘_s’), max($paged, $page))` just refer to a text domain?
- Does wordpress templates always in files or in database?
- Inject class in body when particular page template is used
- Add item to top of menu using a filter in functions.php
- Use WordPress function in php file
- Show meta box only for default page template
- WordPress template page name displayed on screen
- Register a menu – Error Header
- How to call multiple functions from multiple files into a WordPress page template [closed]
- Add login hyperlink to secondary navigation menu
- Function Reference Documenting Template Tags for use in Custom Theme Templates?
- Create page template via functions.php?
- Add value to new attribute inside WordPress menu items
- add_filter function concatenate string and locate_template function
- Template Loop – add switch case php
- Dropdown menu for categories
- Get the name of menu item with wp_nav_menu
- How to add aria role and schema markup to custom walker container
- How can I use custom menus with a Bootstrap WordPress theme?
- Splitting the_content() by size?
- Nav-Menu not showing up
- Remove submenu item from list
- Some problems calling a function into sprintf() inside functions.php template file [closed]
- Display additional page templates and a sidebar on plugin activation
- Dynamic Menu drops pages?
- Menu not styling. New menus functionality giving me a headache
- how to changes mobile menu toggle breakpoint in WordPress
- How to display already created menus via php?
- Providin exception to WordPress wp_nav_menu Custom CSS Classes
- Prevent header and footer from loading on specific pages with template or plugin
- Contain multiple page templates in one PHP custom template file in WordPress?
- Display metabox galleries on specific page template in admin editor
- How to put 2 php codes in functions.php without site crashing
- Menu not updating for logged in users after redirect
- Add element after navigation element title don’t works like I expect
- Add parent menu item in sub-menu in custom nav_walker
- How to include a function in a template with template tag
- remove_action() not working in page template – Genesis
- Fatal error when using array_diff() function inside of wp_update_nav_menu hook?
- Pulling a variable into the wp_nav_menu function
- Calling PHP function doesn’t work in index.php
- How I can add div to menu?
- How to call a certain object/menubar in a PHP file
- WordPress adding in site URL to header links
- Display custom meta box in my template file
- php “use” not working in template [closed]
- Custom Navigation Bar JSON Syntax Error in JSON at Position 0
- How to add different menu items on different menus?
- Error Warning: Declaration of description_walker::start_el after PHP upgrade [duplicate]
- Autoloading in Child Theme
- Help with a custom page template – listing contents of childpages?
- Add a class to the anchor tag on HTML5 gallery
- Add class to group inner container
- Convert the_permalink() function output into string
- blank white page when post/update/change option etc
- Output product category link from WP_Query
- Best way to use variables in multiple functions?
- Cannot fetch posts with certain ‘product_cat’. WP_Query is empty
- Detect what link user clicks and Redirect to a specific page for logged in users only
- Offset with ajax load more posts duplicates
- Why can’t I include echo inside a variable? [closed]
- duplicating page templates