0

I use wp_nav_menu with this code in functions.php:

register_nav_menus( array(
    'sidebar_menu' => 'Sidebar menu',
    'topmenu' => 'topmenu',
) );

$args = array(
    'theme_location' => 'topmenu'
);
wp_nav_menu( $args );

This code works. The menu will show up before everything else in the theme. Now I want to menu inside my header.php for example and I try to move the code there:

$args = array(
    'theme_location' => 'topmenu'
);
wp_nav_menu( $args );

I expected the code to work here as well but it does not. The menu is empty. The function register_nav_menus is still in the functions.php. The header.php is loaded correctly.

The code below returns true in the header.php

if ( has_nav_menu( 'topmenu' ) ) {
    echo 'true';
}

Why does the menu show up empty?

1 Answer 1

1

I had a pre_get_posts. What I missed was this:

&& $wp_query->is_main_query()

This prevents the pre_get_posts to run on the menu query.

Full code

add_action("pre_get_posts", "custom_front_page");
function custom_front_page( $wp_query ){
    if( is_admin() ) {
        return;
    }

    if( is_front_page() && $wp_query->is_main_query() ) {
        $wp_query->set('post_type', 'produkt');
        $wp_query->set('page_id', ''); 

        $wp_query->is_page = 0;
        $wp_query->is_singular = 0;
        $wp_query->is_post_type_archive = 1;
        $wp_query->is_archive = 1;
    }
}

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.