最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

menus - wp_nav_menu() not working on Custom Search Page

programmeradmin5浏览0评论

I'm using WP Custom Search plugin to generate Advanced Search in one of my newly developed WP site. Though the plugin is not completely bug-free, but it's working just fine (excepts some Undefined Offset warnings, I'm ignoring them for its better support in searching).

But recently noticed that the wp_nav_menu() on the header.php is not working when I'm on the search page. It's a simple code as it's used in many of my WP sites:

<?php wp_nav_menu( array( 'theme_location' => 'header_menu', 'menu_class' => 'site-header-menu' ) ); ?>

That's working on all other pages, BUT the search.php - a typical search template.

I've tried the most-cited solution from:

  • this WP Support thread — failed,
  • this SO Thread — failed, and
  • this WPSE thread — failed

Typically the search page contains a ?s= on the URL, but using this plugin, I'm getting:

/?search-class=DB_CustomSearch_Widget-db_customsearch_widget&widget_number=preset-1&[search_queries_and_conditions]=&search=Search

Is that a cause I'm failing echoing the menu? (Live site here, and the Advanced Search is on the left)

I'm using WP Custom Search plugin to generate Advanced Search in one of my newly developed WP site. Though the plugin is not completely bug-free, but it's working just fine (excepts some Undefined Offset warnings, I'm ignoring them for its better support in searching).

But recently noticed that the wp_nav_menu() on the header.php is not working when I'm on the search page. It's a simple code as it's used in many of my WP sites:

<?php wp_nav_menu( array( 'theme_location' => 'header_menu', 'menu_class' => 'site-header-menu' ) ); ?>

That's working on all other pages, BUT the search.php - a typical search template.

I've tried the most-cited solution from:

  • this WP Support thread — failed,
  • this SO Thread — failed, and
  • this WPSE thread — failed

Typically the search page contains a ?s= on the URL, but using this plugin, I'm getting:

http://example/?search-class=DB_CustomSearch_Widget-db_customsearch_widget&widget_number=preset-1&[search_queries_and_conditions]=&search=Search

Is that a cause I'm failing echoing the menu? (Live site here, and the Advanced Search is on the left)

Share Improve this question edited May 23, 2017 at 12:40 CommunityBot 1 asked May 7, 2014 at 18:34 Mayeenul IslamMayeenul Islam 12.9k21 gold badges85 silver badges169 bronze badges 4
  • I tried sultanstationery/?s=test and the menu is being shown. So as you have already figured the plugin probably cause that. Have you tried the wp_get_nav_menu_items function to see what you get when you are on a search page? – Laxmana Commented May 8, 2014 at 17:04
  • It appears to be an issue with the plugin, see this support thread. – Milo Commented May 8, 2014 at 17:06
  • @Laxmana just tried wp_get_nav_menu_items() - it's returning the menu array in all the pages, except the Search page. – Mayeenul Islam Commented May 8, 2014 at 18:39
  • I see. Sorry but I can't think a way to help you more. If you find the solution (as far as I searched there are other people having the same problem) please post it. It would be very useful. – Laxmana Commented May 9, 2014 at 11:50
Add a comment  | 

2 Answers 2

Reset to default 3

I know this is an old thread but the problem still exists. I think this is a bug. Default queries such as nav should not be affected in making custom search page. Anyways, this is how I fixed it:

function fix_nav_menu_in_search($query)
{
    if (is_search()) {
        $query->set('post_type', ['your_cpt', 'nav_menu_item']);
    }

    return $query;
}
add_filter('pre_get_posts', 'fix_nav_menu_in_search');

As Milo stated, it seems it's a problem of the plugin. And solely for this project of mine, I'm not going for such a big dig out, and I'm actually going for a fallback. I repeat, it's not a solution, just a problem hiding measure.

I checked, as Laxmana said that, is there any menu items found there. If there're menu items, show the menu items as a nav_menu, if not, show the default menu (in my case, I'm just showing the "Home" button there - as my site demands that urgently).

        <?php
        $menu_name = 'header_menu';
        $locations = get_nav_menu_locations();
        $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
        $menuitems = wp_get_nav_menu_items( $menu->term_id );
        ?>
        <?php if( !empty($menuitems) ) { ?>
        <?php wp_nav_menu( array( 'theme_location' => 'header_menu', 'menu_class' => 'site-header-menu' ) ); ?>
        <?php } else { ?>
        <div class="menu-top-menu-container">
            <ul class="site-header-menu" id="menu-top-menu">
                <li class="no-margin">
                    <a href="<?php echo home_url('/'); ?>">Home</a>
                </li>
            </ul>
        </div>
        <?php } ?>
发布评论

评论列表(0)

  1. 暂无评论