Custom menu on search page missing
I've been scratching my head over this one, so thanks in advance for any help. Much appreciated.
I've got a menu in WP 3.0.1 which I call in header.php using:
wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) );
It's showing up fine on every page apart from search results. The search.php file starts with a standard get_header();
so it must be the wp_nav_menu
code right? But nothing's wrapped 开发者_StackOverflow社区in an is_search()
conditional or anything.
The output HTML should look like this:
<div class="menu-header">
<ul id="menu-main-menu" class="menu">
<li class="menu-item">
<a>Link 1</a>
</li>
<li class="menu-item">
<a>Link 2</a>
</li>
</ul>
</div>
But instead, it gets as far as the <ul>
and doesn't output any <li>
s:
<div class="menu-header">
<ul id="menu-main-menu" class="menu">
</ul>
</div>
Most strange. Has anyone else come across this before? Is it just far too late and I'm missing something obvious?
@ZoulRic's code works fine, but it breaks the search function in the Media Library. To prevent this add:
if( !is_admin() )
just before the last line, so your code look like:
function menu_fix_on_search_page( $query ) {
if(is_search()){
$query->set( 'post_type', array(
'attachment', 'post', 'nav_menu_item', 'film', 'music'
));
return $query;
}
}
if( !is_admin() ) add_filter( 'pre_get_posts', 'menu_fix_on_search_page' );
You may may have set a filter that is intercepting the query responsible for generating the navigation menu links, or more specifically, overriding the 'nav_menu_item' post type required by the query object.
Try this code:
function menu_fix_on_search_page( $query ) {
if(is_search()){
$query->set( 'post_type', array(
'post', 'nav_menu_item'
));
return $query;
}
}
add_filter( 'pre_get_posts', 'menu_fix_on_search_page' );
Paste this into functions.php in your theme files.
精彩评论