0

I have specific wp menus for each url.
When I encounter a url that has no matching wp menu I dont want any nav. So far it seems if none is found it will show a default nav or pick one of the existing navs to show.

use case

http://domainnameA.com show wp_nav_menu('domainnameA')

http://domainnameB.com no menu exists - dont show a nav

instead I am getting Nav on Domain B that has a link to the only page that exists. If I delete the page I get no navigation but that is not a great solution.

1
  • would be great if you show us the piece of code you are using :) Commented Nov 17, 2011 at 4:00

1 Answer 1

1

Use has_nav_menu

e.g.

$menu_name=$_SERVER['SERVER_NAME'];
$menu_name=str_ireplace("WWW.","",$menu_name);
$menu_name=str_ireplace(".","",$menu_name);
$menu_name = sanitize_title($menu_name); // sanitize!

if(has_nav_menu($menu_name)){
    wp_nav_menu(array('menu' => $menu_name));
}
9
  • this is close I am dynamically selecting my menus based on siteurl so I am using a variable to call the menu name. any idea if this would work with a variable instead of text?
    – aj martin
    Commented Nov 21, 2011 at 6:15
  • There's absolutely no reason not, afterall 'primarymenu' is just a hardcoded string variable
    – Tom J Nowell
    Commented Nov 21, 2011 at 10:08
  • `<?php $menu_name=$_SERVER['SERVER_NAME']; $menu_name=str_ireplace("WWW.","",$menu_name); $menu_name=str_ireplace(".","",$menu_name); ?> <div id="nav"> <?php wp_nav_menu( array('menu' =>$menu_name )); ?> </div>
    – aj martin
    Commented Nov 21, 2011 at 14:14
  • when I tried if(has_nav_menu($menu_name)) or if(has_nav_menu(array('menu' =>$menu_name ))) didnt seem to work
    – aj martin
    Commented Nov 21, 2011 at 14:18
  • that seems like a hackish way of generating $menu_name, for one have you checked the value of $menu_name and registered a menu of that name in your functions.php? You also forgot the http://
    – Tom J Nowell
    Commented Nov 21, 2011 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.