1

My theme need to render 2 menus in homepage. The rest pages only need one. As result is only homepage got duplicate query warning. After tracking down, I found out this was cause by wp_nav_menu. How do I avoid this problem?

Here is the screenshot of my duplication warning: The duplicated query

SELECT t.*, tt.*
FROM wp_terms AS t
INNER JOIN wp_term_taxonomy AS tt
ON t.term_id = tt.term_id
WHERE t.term_id = 163

And its caller:

  1. Lucidy\header()
    wp-content/themes/lucidy/engines/linker.php:86
  2. Lucidy\render()
    wp-content/themes/lucidy/engines/linker.php:79
  3. Lucidy\load()
    wp-content/themes/lucidy/engines/linker.php:48
  4. wp_nav_menu()
    wp-includes/nav-menu-template.php:120
  5. wp_get_nav_menu_object()
    wp-includes/nav-menu.php:26
  6. get_term()
    wp-includes/taxonomy.php:834
  7. WP_Term::get_instance()
    wp-includes/class-wp-term.php:131

According to debugger, potential troublemakers are get_term_link and get_term_field. However, I can be very sure that I don't directly use them. It must be called from the wp_nav_menu. The wp_nav_menu is the main suspect because in other page, such as archive page or singular page, I couldn't find any duplication warning.

Is this the core bug? Anyway, how do I avoid it? My theme has been slow recently (up to 0.30s~0.60s rendering time for homepage) so I would like to optimize it very much.

[Update 5-Otc] Sorry, I forgot to share my menu info. I actually has 3 menu locations and 2 of them are placed in homepage. At homepage I use wp_nav_menu 2 times.

I register those in functions.php:

register_nav_menus([
    'menu_col' => __('Menu Column', 'lucidy'),
    'menu_list' => __('Menu List', 'lucidy'),
    'menu_bar' => __('Menu Bar', 'lucidy')
]);

1 Answer 1

0

You'd avoid it by creating another menu and menu location. You can do this with a widget area or whatever. Then only call the menu if the page is the home page with is_home() or is_front_page() if it is a page.

Here's from twenty seventeen as an example.

register_nav_menus(
    array(
        'top'    => __( 'Top Menu', 'twentyseventeen' ),
        'social' => __( 'Social Links Menu', 'twentyseventeen' ),
    )
);

and then it's called like:

<?php if ( has_nav_menu( 'top' ) ) : ?>
    <div class="navigation-top">
        <div class="wrap">
            <?php get_template_part( 'template-parts/navigation/navigation', 'top' ); ?>
        </div>

You can pretty much put whatever you want in widget areas though. I use them just incase I wanna stick something else there.

In your situation, menu is the "correct" way to do it though.

2
  • Sorry, I forgot to share my menu info. I have update my question for detail. I have 2 menu locations in homepage called by wp_nav_menu. Because of that, I'm not quite understand the part of creating another menu and its location. Also, what does widget area get involved in here, I thought widget for plugin and HTML components? Could you help me to understand with some sample code?
    – Star Light
    Commented Oct 5, 2019 at 3:37
  • You can add menus to widget locations. Or, create another menu. I'm going to add the example to the answer. Commented Oct 8, 2019 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.