wp_nav_menu() returns empty when query string contains certain parameters

拥有回忆 提交于 2019-12-11 20:00:59

问题


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


来源:https://stackoverflow.com/questions/19321866/wp-nav-menu-returns-empty-when-query-string-contains-certain-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!