WordPress.org

Ready to get started?Download WordPress

Forums

WP Nav Menu Dissapears in Category Pages! (21 posts)

  1. dtomasch
    Member
    Posted 3 years ago #

    I’ve got a stumper for you —

    On:

    http://www.diplomatswashington.com/view/wordpress/about

    I have several WP Nav Menus. They work just fine on pages and on single.php

    However, under:

    http://www.diplomatswashington.com/view/wordpress/category/events/advancedfocused-morning-programs

    All the menus disappear! They disapear under any archive.php or category.php page — but show up everywhere else. Any suggestions?

  2. esmi
    Theme Diva & Forum Moderator
    Posted 3 years ago #

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

  3. dtomasch
    Member
    Posted 3 years ago #

    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.

  4. esmi
    Theme Diva & Forum Moderator
    Posted 3 years ago #

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

  5. Designophobia
    Member
    Posted 3 years ago #

    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.

  6. Designophobia
    Member
    Posted 3 years ago #

    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?

  7. Developer ICAN
    Member
    Posted 3 years ago #

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

  8. Developer ICAN
    Member
    Posted 3 years ago #

    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'));
    ?>

  9. nickkeenan
    Member
    Posted 3 years ago #

    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!

  10. nickkeenan
    Member
    Posted 3 years ago #

    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?

  11. jian118
    Member
    Posted 3 years ago #

    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.

  12. kerssies
    Member
    Posted 2 years ago #

    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!

  13. jian118
    Member
    Posted 2 years ago #

    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

  14. clareswindlehurst
    Member
    Posted 2 years ago #

    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!

  15. boyvanamstel
    Member
    Posted 2 years ago #

    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'

  16. Umbrovskis.com
    Member
    Posted 2 years ago #

  17. thanushka
    Member
    Posted 2 years ago #

    Thank You! Developer ICAN.
    It worked like a charm for me.
    This is what I did.
    In the template file, (mine was header.php) where you call wp-nav-menu()
    Paste the below snippet ABOVE the wp-nav-menu() code snippet starts.

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

  18. creariz
    Member
    Posted 2 years ago #

    Hi, I'm having the same problem for a few weeks now. I'm glad I found this blog, because I was totally lost. My primary menu shows perfect with page.php and single.php but it doesn't work with category.php

    I'm trying to implement jian118 solution but I can't image how am I gonna make getMainMenu()? cause wp_nav_menu() prints directly and I can't get a return value to evaluate true or false. I know that probably I have the solution in front of me but I can't see it.

    I have issues finding the solution to this problem because I really don't know why it is happening.

    I found this post by Ronald Huereca: http://weblogtoolscollection.com/archives/2008/04/19/paging-and-custom-wordpress-loops.

    It explains why pagination doesn't work on category.php. He says that next_posts_link and previous_posts_link functions, as well as several others, make use of a global variable called $wp_query. This variable is actually an instance of the WP_Query object. However, when we create our own instantiation of WP_Query, we don’t have this global variable $wp_query, which is why paging will not work.

    I think that could be a good explanation if only I could understand how $wp_query works, when it is instantiated, and why is it that when the category.php calls the header.php the menu doesn't work, or wp_query is not ready.

    I posted this commentary just to ask for some help for make getMainMenu(), sorry if just made a lot of questions. Thanks.

  19. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

  20. jian118
    Member
    Posted 2 years ago #

    function getMainMenu(){
    	$locations = get_nav_menu_locations();
    	$menuItems = wp_get_nav_menu_items( $locations[ 'primary' ] );
    	if(empty($menuItems))
    		return false;
    	else{
    		/*My own custom menu*/
    		return true;
    	}
    }
  21. creariz
    Member
    Posted 2 years ago #

    Thank you esmi and jian118. I just solved the problem thanks to you guys.

    This is the code:

    functions.php

    function getMainMenu($menulocation){
      $locations = get_nav_menu_locations();
      $menuItems = wp_get_nav_menu_items( $locations[ $menulocation ] );
        if(empty($menuItems))
          return false;
        else{
          wp_nav_menu(array('theme_location' => $menulocation));
          return true;
        }
    }

    template file

    if(!getMainMenu('primary')){
      $backup = $wp_query;
      $wp_query = NULL;
      $wp_query = new WP_Query(array('post_type' => 'post'));
      getMainMenu('primary');
      $wp_query = $backup;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic