If you’re like me and you spend time making secondary, tertiary, quaternary navs and breadcrumbs then you know the benefit of a single menu hierarchy be it in the form of organizing pages in parent->child relationships or, as I usually do, giving users one massive menu to manage. Benefits of this are multiple: you can include damn near anything (pages, posts, tags, authors, post type archives, regular archives, the works), but the trouble then comes from when an archive isn’t in that single menu hierarchy and everything starts to go wrong. Here’s how we can handle that: sniff out whether or not the current page exists in a menu! Just drop the function below into your functions.php file and you’re ready to go:

function page_in_menu( $menu = null, $object_id = null ) {
    $menu_object = wp_get_nav_menu_items( esc_attr( $menu ) );
    if( ! $menu_object )
        return false;
    $menu_items = wp_list_pluck( $menu_object, 'object_id' );
    if( !$object_id ) {
        global $post;
        $object_id = get_queried_object_id();
    }
    return in_array( (int) $object_id, $menu_items );
}

So what this does and how it works is simple: place the above snippet and include a menu slug and/or an object ID (this can be a post ID, for example). If no object ID is included then the script will grab the current query’s ID; and if no menu slug is included then the function just will return false, simple as that. So with that in mind you can see how you would call that:

if(page_in_menu('primary_nav',12)){
     echo "In the menu!";
}else{
     echo "Not in the menu!";
}

The above is checking whether the post with ID 12 exists within the menu slugged primary_nav and if it is it’ll echo In the menu! and if not it’ll echo Not in the menu!. So you can see the general gist. Another example, as I’m currently using it in a project right now:

<?php $flb = ''; ?>
<div id="content" class="<?php if(!page_in_menu('primary_nav')){
                                 $flb = 'default';
                                 echo "default";} ?>">
     <?php if(has_nav_menu('primary-menu')){
        $defaults = array(
            'theme_location'  => 'primary-menu',
            'menu'            => 'primary-menu',
            'container'       => 'div',
            'container_class' => $flb,
            'container_id'    => 'pageNav',
            'menu_class'      => 'fallback',
            'echo'            => true,
            'fallback_cb'     => 'wp_page_menu',
            'link_after'      => '<div class="colorswipe"></div>',
            'items_wrap'      => '<ul id="%1$s">%3$s</ul>'
        );
        wp_nav_menu( $defaults ); } ?>
</div>

With the above I’m checking whether the current page exists in primary_nav and if it doesn’t then I’m setting $flb as default and echoing default to the #content element. When calling the wp_nav_menu I’m then adding in $flb to the container_class as a second level to check whether the page exists in that menu. Add some CSS to that and I can hide/show whatever part of the menu I need to, even if it doesn’t technically exist in there. This is most useful when you’re dynamically rendering and hiding particular sections of a menu based on the level and/or ancestry of that particular active menu item.