Zach Adams Web Developer and Programmer

How to Fix Navigation Menu not showing up on Category page

December 3, 2014

I ran into a very interesting problem working yesterday, with a very strange answer.

First I had a custom post type with it’s own categories, in order to get that working I had to include some code in my functions.php allowing the query to be correctly sent to the category page:

function namespace_add_custom_types( $query ) {
    if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] && $query->is_main_query() ) ) {
        $query->set( 'post_type', array(
            'post', 'menu'
        ));
        return $query;
    }
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

The “menu” being the custom post type. This was all well and good and my categories were working fine until I noticed that my navigation menus wouldn’t show up on those pages. Very weird. After a little debugging I Googled it and found a curious answer by boyvanamstel. Basically all you have to do is add “nav_menu_item” to post_types passed to the query like so:

function namespace_add_custom_types( $query ) {
    if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] && $query->is_main_query() ) ) {
        $query->set( 'post_type', array(
            'post', 'menu', 'nav_menu_item'
        ));
        return $query;
    }
}

And that fixed my issue immediately. Let me know if this worked for you!

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *