5

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?

8
  • Please edit your question to add the code where you register your dynamic sidebars. Commented Oct 8, 2013 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
    Commented Oct 8, 2013 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). Commented Oct 8, 2013 at 17:16
  • Sorry for the confusion. I meant a template-part file.
    – justlemons
    Commented Oct 8, 2013 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
    Commented Oct 8, 2013 at 17:25

2 Answers 2

10

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' ) );
}
6
  • 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
    Commented Oct 8, 2013 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(). Commented Oct 8, 2013 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. :) Commented Oct 8, 2013 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
    Commented Oct 8, 2013 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
    Commented Oct 8, 2013 at 21:35
1

I think I'm doing a similar thing to the OP: letting my client (wp admin user) create Menus (in WP-Admin > Appearance > Menus), which are NOT assigned to a specific theme location, because that requires a static / one-to-one relationship.

Instead, these Menus are displayed dynamically (by a custom widget), based on the name of the current-page's top-ancestor. If that name matches an existing Menu's name, show the Menu (wrapped by standard widget before/after html). But, if there's NO match, output nothing.

So I needed to check if the Menu exists, before starting widget output.

$args = array(
    'menu' => $menu_name, // from top-ancestor of current page
    'echo' => 0,  // true or 0
    'theme_location' => '__fake', // fake value, to prevent display of 1st real menu
    'fallback_cb' => false  // prevent display of 1st real menu
);
$menu = wp_nav_menu( $args );
if ($menu){ 
    # output
    echo $before_widget; // Before
    echo $before_title . $title . $after_title; // Title
    echo $menu;  
    echo $after_widget; // After
}

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.