Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 794
SqrBrkt Avatar asked Sep 14 '25 08:09

SqrBrkt


2 Answers

@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' );
like image 164
elbatron Avatar answered Sep 17 '25 19:09

elbatron


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.

like image 37
ZoulRic Avatar answered Sep 17 '25 18:09

ZoulRic