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'm currently retrieving a nav menu based on the parent page's title, and if the page doesn't have a parent then it's own title.

global $post;
$page_title;

if ($post->post_parent) {
    $page_title = get_the_title($post->post_parent);
}  else {
    $page_title = get_the_title($post);
}

$sidebar_title = 'Sidebar - '.$page_title;
wp_nav_menu(array( 'menu' => $sidebar_title));

Instead of first checking for a parent, I'd like to first check if a page has it's own unique menu. This is what I wrote but it doesn't work:

global $post;
$page_title = get_the_title($post);
$sidebar_title = 'Sidebar - '.$page_title;

if ( !wp_nav_menu(array( 'menu' => $sidebar_title, 'echo' => false )) ) {

    $page_title = get_the_title($post->post_parent);
    $sidebar_title = 'Sidebar - '.$page_title;
}

wp_nav_menu(array( 'menu' => $sidebar_title ));

TLDR: How do I check if a menu exists?

share|improve this question
    
Please edit your question to add the code where you register your dynamic sidebars. –  Chip Bennett Oct 8 '13 at 16:37
    
I didn't register this as a dynamic sidebar. Do I need to? I've just been naming my sidebar files sidebar-name.php and then using get_sidebar('name') when needed. –  justlemons Oct 8 '13 at 17:13
1  
Okay, what do you mean when you use the term sidebar. It can mean at least 3 things: 1) a content sidebar (HTML), 2) a Widgetized area (dynamic_sidebar()), or 3) a template-part file (get_sidebar()/sidebar.php). –  Chip Bennett Oct 8 '13 at 17:16
    
Sorry for the confusion. I meant a template-part file. –  justlemons Oct 8 '13 at 17:19
    
Oh my, I realize how confusing that was now. I'm working with Wordpress' navigation menus. I just happen to be placing it in a sidebar. –  justlemons Oct 8 '13 at 17:25

1 Answer 1

Assuming that you have custom Nav Menus implemented properly:

  1. Registering nav menu Theme Locations:

    register_nav_menus( array(
        'parent_page' => "Parent Page",
        'page_a' => "Page A",
        'page_b' => "Page B", //etc
    ) );
    
  2. Calling wp_nav_menu() correctly:

    wp_nav_menu( array(
        'theme_location' => 'page_a'
    );
    

...then you can use the has_nav_menu() conditional to determine if a Theme Location has a menu assigned to it:

if ( has_nav_menu( 'page_a' ) ) {
    // do something
}

In your specific case, you could do something like so:

if ( has_nav_menu( 'page_a' ) ) {
    wp_nav_menu( array( 'theme_location' => 'page_a' ) );
} else {
    wp_nav_menu( array( 'theme_location' => 'parent_page' ) );
}
share|improve this answer
    
Thank you for your response. This is the first website I've ever developed using Wordpress, and I wasn't aware of needing to register my nav menus. I read the material you suggested. Still, I was able to solve my problem without registering them. I think it works for my purposes since my client won't need the ability to manage nav menu locations. If you think this will cause me problems down the road, please let me know. –  justlemons Oct 8 '13 at 21:19
    
"my client won't need the ability to manage nav menu locations" - then why have nav menus at all? (No: seriously.) Why not just use wp_list_pages() or wp_page_menu()? Because if the user isn't defining nav menus, then there's literally no reason at all to use wp_nav_menu(). –  Chip Bennett Oct 8 '13 at 21:21
    
Also: I'm confused by your question's title: how to tell if a menu exists?. If the user isn't defining menus, how will menus ever exist? I suspect that there's something else, deeper, that you're really trying to accomplish - and I suspect that there's probably an easier way than the way you're currently going about it. :) –  Chip Bennett Oct 8 '13 at 21:23
    
Even though my client won't be managing the location of menus, he'll still have the ability to add new items to those menus if he so chooses. –  justlemons Oct 8 '13 at 21:25
    
I have several pages on the site and each parent page has a unique nav menu. Some children pages have unique nav menus as well but not all. For those children pages that do not have unique nav menus, then I want them to use their parent's nav menu. So, what I'm checking for is if the unique nav menu exists so I can display it, and if the page doesn't have a unique nav menu, then I want to display it's parent page's nav menu. –  justlemons Oct 8 '13 at 21:35

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.