Viewing 5 replies - 16 through 20 (of 20 total)
  • 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’));
    ?>

    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: https://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.

    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;
    	}
    }

    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;
    }
Viewing 5 replies - 16 through 20 (of 20 total)
  • The topic ‘WP Nav Menu Dissapears in Category Pages!’ is closed to new replies.