WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] No menu on archive pages (9 posts)

  1. nikospkrk
    Member
    Posted 1 year ago #

    Hi Chouby,
    My category, page and custom menu widgets are not working (empty) on my archive pages ('/cat/something/') when I add the following in my function.php file:

    //Add all post types
    	add_action('pre_get_posts', 'my_posts');
    	function my_posts($query) {
    		if (is_tag() || is_category()) {
    			$qvars = $query->query_vars;
    			$qvars['post_type'] = 'any';
    			$query->parse_query($qvars);
    		}
    	}

    I'm not using the actual WP posts and I wanted to add my custom types ('projects') to the $query variable. I tried deactivating all plugins but yours and still the same issue happens.

    If you're wondering, I'm calling the menu that way:
    <?php wp_nav_menu( array( 'container_class' => '', 'theme_location' => 'header-menu' ) ); ?>

    I'm using the latest versions of WP (3.4.2) and polylang (0.9.2), any idea what's happening there? Maybe there's something wrong with my function?

    Cheers,

  2. Chouby
    Member
    Posted 1 year ago #

    Maybe I did not understand what you expect, but your function resets the query and so the result will depend on whether it is executed before or after the function hooked to the the same filter in Polylang. Moreover 'pre_get_posts' is a filter and not an action. Does the snippet below do what you want?

    add_filter('pre_get_posts', 'my_posts');
    function my_posts($query) {
    	if (is_tag() || is_category()) {
    		$query->set_query('post_type', 'any');
    	}
    	return $query;
    }
  3. nikospkrk
    Member
    Posted 1 year ago #

    Sorry, I thought it was an action, as you told me here.

    Anyway, your snipet doesn't work as it seems there's no set_query() function/method :

    Fatal error: Call to undefined method WP_Query::set_query()

    Thanks for the support!

  4. nikospkrk
    Member
    Posted 1 year ago #

    I got that error fixed by using the following code:

    $query->set('post_type', 'any');

    But there's still no menu (inserted manually in header.php) and pages (widget in my bottom sidebar). Any idea why?

    Cheers,

  5. Chouby
    Member
    Posted 1 year ago #

    Sorry for the bad information on the filter which is not an action, and for the bad name of the function.

    Do you get all your posts and projects in the right language?

    Did you register your menu theme location using register_nav_menu? Is your theme location visible in the Settings->languages->menus admin panel?

  6. nikospkrk
    Member
    Posted 1 year ago #

    That's fine, thanks for helping :)

    Yes all the posts are in the right language, and my language-based widgets are working fine.

    Yes I did:

    //Register menus
    	function registerMenus() {
    		register_nav_menus(
    			array( 'header-menu' => __( 'Header Menu', 'vd' ) )
    		);
    	}
    	add_action( 'init', 'registerMenus' );

    Yes it is.

    Everything's working fine on all the other pages, really.

  7. Chouby
    Member
    Posted 1 year ago #

    I made further tests and finally discovered that it is quite obvious that your function breaks the menu since get_posts is called to get nav_menu items. In that case, the post type is set to nav_menu_item and is replaced by 'any'.
    Maybe you should include an additional test more or less like this:
    add_filter('pre_get_posts', 'my_posts');

    function my_posts($query) {
    	if ((is_tag() || is_category()) && (!isset($query->query_vars['post_type']) || !$query->query_vars['post_type'])) {
    		$query->set_query('post_type', 'any');
    	}
    	return $query;
    }
  8. nikospkrk
    Member
    Posted 1 year ago #

    Adding your condition worked, thanks!
    My function code is finally:

    add_filter('pre_get_posts', 'my_posts');
    function my_posts($query) {
    	if ((is_tag() || is_category()) && (!isset($query->query_vars['post_type']) || !$query->query_vars['post_type'])) {
    		$query->set('post_type', 'any');
    	}
    	return $query;
    }

    I got confused because I thought that this pre_get_posts() was only used for the posts in the page, not the widgets & menus.

    Thanks!

  9. Saharnava
    Member
    Posted 1 year ago #

    Thank you nikospkrk, you just save my day :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags