Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
would be great if you show us the piece of code you are using :) –  andresmijares25 Nov 17 '11 at 4:00

1 Answer 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));
}
share|improve this answer
    
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 Nov 21 '11 at 6:15
    
There's absolutely no reason not, afterall 'primarymenu' is just a hardcoded string variable –  Tom J Nowell Nov 21 '11 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 Nov 21 '11 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 Nov 21 '11 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 Nov 21 '11 at 15:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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