0

I'm developing a new theme and am encountering a weird behaviour.

I have registered a footer menu:

register_nav_menus([
                'primary_navigation' => "Main nav",
                'footer_navigation' => "Footer nav"
                ]);

In the template, primary_navigation menu shows correctly the custom menu created in the Administration zone.

But footer_navigation does not. In fact, it returns false:

if (has_nav_menu('footer_navigation')) {
?>
    <ul class="c-footnav__list flex-container" role="navigation">

<?php   

    if(!wp_nav_menu( array( 'theme_location' => 'footer_navigation', 'depth'=>10, 'echo'=>true ))){

    echo "could not print menu";    
        }; ?>

    </ul>

<?php
 }

It prints the <UL> element and the "could not print menu" message.

The documentation regarding wp_nav_menu() says:

(object|false|void) Menu output if $echo is false, false if there are no items or no menu was found.

So I must assume that it does not find the menu. But why then the call to has_nav_menu('footer_navigation') returns true ? What am I missing?

0

1 Answer 1

1

It turns out the fault was all mine.

I was attaching a callback function to the wp_nav_menu_items hook that returned empty if it wasn't the main menu... This was it:

// Add logo in main menu

 add_filter( 'wp_nav_menu_items', 'add_logo_in_main_menu', 10, 2 );



function add_logo_in_main_menu( $items, $menu ){
    /*
        Append logo to main menu
    */
        if(is_admin() ||  'primary_navigation' !== $menu->theme_location){
            return;  //<!--- in this case, returns nothing
        }
        return '<li class="c-logo--medium"><img src="'.get_stylesheet_directory_uri().'/assets/images/logo-pm.jpg" alt=""></li>'. $items;

    }

The fix was easy:

function add_logo_in_main_menu( $items, $menu ){
        /*
        Append logo to main menu
        */
        if(is_admin() ||  'primary_navigation' !== $menu->theme_location){
            return $items;
        }
        return '<li class="c-logo--medium"><img src="'.get_stylesheet_directory_uri().'/assets/images/logo-pm.jpg" alt=""></li>'. $items;

    }

Sorry for the noise.

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.