问题描述
我一直在抓头,所以在此先感谢您的帮助.非常感谢.
I've been scratching my head over this one, so thanks in advance for any help. Much appreciated.
我在WP 3.0.1中有一个菜单,该菜单在header.php中使用:
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' ) );
除了搜索结果除之外,每个页面上的显示情况都很好. search.php文件以标准get_header();
开头,因此它必须是wp_nav_menu
代码,对吗?但是is_search()
有条件的东西什么也没包裹.
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 in an is_search()
conditional or anything.
输出的HTML应该如下所示:
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>
但是,它可以到达<ul>
,并且不会输出任何<li>
:
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的代码可以正常工作,但是它破坏了媒体库中的搜索功能.为防止这种情况,请添加以下内容:
@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' );
这篇关于搜索页面上的自定义菜单丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!