WordPress.org

Support

Support » Themes and Templates » WP Nav Menu Dissapears in Category Pages!

WP Nav Menu Dissapears in Category Pages!

Viewing 15 replies - 1 through 15 (of 20 total)
  • esmi

    @esmi

    Forum Moderator

    Have you checked the relevant template files (category.php or archive.php)?

    Yep. It’s odd b/c wp_nav_menu is called from the header and the footer, so it’s exactly the same call as on the other pages.

    esmi

    @esmi

    Forum Moderator

    Are you using any custom menus? If not, is there a custom callback for wp_nav_menu in functions.php that is using conditionals?

    Designophobia

    @designophobia

    I’m having the same problem. Here’s my setup:

    I’m using a hacked version of Twenty Ten theme. I have one custom menu that I’m using for the primary menu. It displays properly on all the pages and posts including the home page. The menu block is empty on all archive pages including tag and category pages. When I change the Primary Navigation to the default menu in the Theme Locations box, the default menu displays properly site wide. I have even deactivated and reactivated my plugins to no avail.

    Please help! I’m lost.

    Designophobia

    @designophobia

    Well I found my problem, but fixing it causes another problem.

    I have a custom post type named “articles” to separate them from the pages and posts. They were not showing up in the category or tag pages. To fix this, I inserted the following code in the functions file:

    add_filter('pre_get_posts', 'query_post_type');
    function query_post_type($query) {
      if(is_category() || is_tag()) {
        $post_type = get_query_var('post_type');
    	if($post_type)
    	    $post_type = $post_type;
    	else
    	    $post_type = array('post','articles');
        $query->set('post_type',$post_type);
    	return $query;
        }
    }

    Adding this code allows the custom post types to show up in the tag and category pages, but it makes the custom menu dissappear. Is there any way to rewrite the above code so that everything works like I need it to?

    Developer ICAN

    @richardicanie

    hi have you found a solution to this? – this is EXACTLY the situation i’m in right now?

    Developer ICAN

    @richardicanie

    hi i’ve found a solution – if you alter the query at the bottom of your page or just before you call wp-nav-menu(), everything works again ..

    <?
    $wp_query = NULL;
    $wp_query = new WP_Query(array(‘post_type’ => ‘post’));
    ?>

    FASCINATING.

    It seems as though I’m following you down a rabbit hole. Clearly the above code is not quite the right solution, but thanks for this monkey patch!

    This thread of work around was giving me some problems (though I was learning a lot!). Rather than add a WP_Query filter, I decided to adapt my theme pages (like category.php) with a less intrusive query change than the filter solution.

    global $query_string;
    parse_str($query_string, $args);
    $args['post_type'] = array('post','articles');
    query_posts( $args );

    This pulls the existing query and just adds your custom post type to the existing query.

    This allowed a bunch of other related core functions to work properly – my custom menus, permalinks, rewrites, etc. Maybe it’ll work for you?

    It works! Thanks.
    But i have a better solution for those who have their own function to get the menu.
    if(!getMainMenu()){
    $backup=$wp_query;
    $wp_query = NULL;
    $wp_query = new WP_Query(array(‘post_type’ => ‘post’));
    getMainMenu();
    $wp_query=$backup;}

    notes:My getMainMenu function returns false when it fails to retrieve menu.

    With this way, i don’t need to adapt the solution in every theme files that requires it.
    Hope it helps.

    I was looking for this for a while. I can follow up to Designophobia’s post.

    @jian118 In which file should I paste you code?
    Thanks for your help!

    u have to create your own getMainMenu function in functions.php
    ur getMainMenu function should return true or false

    the code i post above should be pasted in where every u need to load the navigation menu.

    hope this helps

    I feel like I’ve been going round in circles on this one for weeks with menus appearing and disappearing. I’ve now ditched the filter from the functions and inserted nickkeenan’s code into the loop for category, tag and home and all seems to be ship shape!

    This fixed it for me: just add ‘nav_menu_item’ to the post types in the filter method provided above.

    add_filter('pre_get_posts', 'query_post_type');
    function query_post_type($query) {
      if(is_category() || is_tag()) {
        $post_type = get_query_var('post_type');
    	if($post_type)
    	    $post_type = $post_type;
    	else
    	    $post_type = array('nav_menu_item','post','articles');
        $query->set('post_type',$post_type);
    	return $query;
        }
    }

    Exact method I use: https://gist.github.com/1055880

    EDIT: I meant ‘nav_menu_item’

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘WP Nav Menu Dissapears in Category Pages!’ is closed to new replies.
Skip to toolbar