I’m trying to use wp_nav_menu to only display a menu if one exists, otherwise, display nothing.
If I delete the menu, it will output a list of the pages.
My functions.php file contains:
if (function_exists('register_nav_menus')) {
register_nav_menus (
array('main_nav' => 'Main Navigation Menu'));}
How can I use wp_nav_menu to only display a menu if one exists, otherwise show nothing?
Use
has_nav_menu()
, and test fortheme_location
, rather thanmenu_id
:You can output alternate content, by adding an
else
clause.EDIT
You need to replace
$theme_location
with your actualtheme_location
:You can just specify false as the
fallback_cb
argument ofwp_nav_menu
. Nothing will show up — rather, wp_nav_menu will return false (echoing nothing out).You can just register menu firstly without specifying the location.
In
functions.php
:And when you call the menu in
header.php
, check withhas_nav_menu()
:Read this:
http://codex.wordpress.org/Function_Reference/wp_nav_menu
set ‘fallback_cb’ => false and none will be displayed, this is the standard method suggested by wp guys.