wp_nav_menu() returns empty when query string cont

2019-09-16 03:45发布

问题:

I'm having this strange glitch with a nav menu obtianed through wp_nav_menu().

The function returns the menu correctly in every situation except for when the query string in an ARCHIVE request includes the 'cat' parameter. For example:

I have a custom post type 'Stories' for which the archive is just myhomeurl.com/story. For this page, wp_nav_menu() will return the menu correctly and as such will display as it should.

But if I want to see just the stories with a certain category (i.e. myhomeurl.com/story/?cat=5), then wp_nav_menu() will not return anything. It's not a display issue, it is just that wp_nav_menu() will not return any markup.

I have no idea what could be causing this, all I have been able to figure out is those conditions: in an archive with the category parameter set in the query string.

Just in case it is relevant, I am using a customized child theme of the 'Responsive' theme.

Unfortunately this is a local build so I can't really link to the site itself, I hope that is not an issue.

Thank you in advance for any help.

回答1:

I was having this same issue too. It seems that the custom post types are getting mixed up with your navigation. If you wrap your navigation code with this, it should work:

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

        //nav code goes here

        $wp_query = $backup; ?>