Nav menu works in header.php but not in templates

Question

I’m using this to add a menu to a nav bar on my pages. This is included in the header.php file.

<?php 
if ( current_user_can( 'manage_options' ) ) {
                            wp_nav_menu( array(
                            'container' => 'menu',
                            'container_class' => '',
                            'menu_class' => 'dropdown',
                            'menu_id' => 'mainmenu',
                            'sort_column' => 'menu_order',
                            'theme_location' => 'logged-in-admin',
                            'walker' => new Description_Walker
                            )); 
            }
    elseif (is_user_logged_in()) {
                            wp_nav_menu( array(
                            'container' => 'menu',
                            'container_class' => '',
                            'menu_class' => 'dropdown',
                            'menu_id' => 'mainmenu',
                            'sort_column' => 'menu_order',
                            'theme_location' => 'logged-in',
                            'walker' => new Description_Walker
                            )); }
 else {
                            wp_nav_menu( array(
                            'container' => 'menu',
                            'container_class' => '',
                            'menu_class' => 'dropdown',
                            'menu_id' => 'mainmenu',
                            'sort_column' => 'menu_order',
                            'theme_location' => 'logged-out',
              'add_a_class' => 'nav-link',
                            'walker' => new Description_Walker
                            ));
                         }
?>

But when I copy/paste this same code to a template file (not using get_header(), but starting with the html tag after the template tags), it looks like it doesn’t detect that I’m an Admin.

The role-administrator tag is present in the body tag so WordPress knows I’m an admin.
All menus are registered.

The result is that I get the Menu for logged-out out users, not even for logged-in users, let alone for logged-in-admin user.

When I add this to the page then the logged-in Menu shows up.

<?php 
    if (is_user_logged_in()) {
                            wp_nav_menu( array(
                            'container' => 'menu',
                            'container_class' => '',
                            'menu_class' => 'dropdown',
                            'menu_id' => 'mainmenu',
                            'sort_column' => 'menu_order',
                            'theme_location' => 'logged-in',
                            'walker' => new Description_Walker
                            ));
} else {
                            wp_nav_menu( array(
                            'container' => 'menu',
                            'container_class' => '',
                            'menu_class' => 'dropdown',
                            'menu_id' => 'mainmenu',
                            'sort_column' => 'menu_order',
                            'theme_location' => 'logged-out',
                            'walker' => new Description_Walker
                            ));
                         }
    ?>

Does anyone has an idea why this is happening?

0
WendiT 2 months 2023-03-22T06:10:31-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse