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

functions - Nav menus all outputting the same links

programmeradmin1浏览0评论

I have registered the following menus in my functions.php

// register menus
function register_menus() {
    register_nav_menus(
        array(
            'footer-01' => 'Footer 01',
            'footer-02' => 'Footer 02',
            'footer-04' => 'Footer 04'
        )
    );
}
add_action('init', 'register_menus');

I've set them up with their each own set of links.

Now I've put the following into my theme files to show them on the pages:

<?php wp_nav_menu( 'footer-01' ); ?>
<?php wp_nav_menu( 'footer-02' ); ?>
<?php wp_nav_menu( 'footer-04' ); ?>

But it seems that each menu outputs the same set of links, they all show the links that have been added to the footer-04 menu

How do I get menus 01 and 02 to show their own links?

I have registered the following menus in my functions.php

// register menus
function register_menus() {
    register_nav_menus(
        array(
            'footer-01' => 'Footer 01',
            'footer-02' => 'Footer 02',
            'footer-04' => 'Footer 04'
        )
    );
}
add_action('init', 'register_menus');

I've set them up with their each own set of links.

Now I've put the following into my theme files to show them on the pages:

<?php wp_nav_menu( 'footer-01' ); ?>
<?php wp_nav_menu( 'footer-02' ); ?>
<?php wp_nav_menu( 'footer-04' ); ?>

But it seems that each menu outputs the same set of links, they all show the links that have been added to the footer-04 menu

How do I get menus 01 and 02 to show their own links?

Share Improve this question asked Jun 20, 2020 at 11:42 user39214user39214
Add a comment  | 

1 Answer 1

Reset to default 1

I have registered the following menus in my functions.php

Note that register_nav_menus() which uses register_nav_menu() (note the singular "menu") does not register or create a navigation menu; it only registers navigation menu locations which you can assign one or more (navigation) menus to, and for displaying the menus assigned to a specific location, you can use wp_nav_menu() just as you've already tried.

How do I get menus 01 and 02 to show their own links?

The problem in your wp_nav_menu() call is because you're not specifying the proper navigation menu location which is identified by the theme_location parameter like so:

<?php wp_nav_menu( 'theme_location=footer-01' ); ?>
<?php wp_nav_menu( 'theme_location=footer-02' ); ?>
<?php wp_nav_menu( 'theme_location=footer-04' ); ?>

More details based on the wp_nav_menu()'s reference:

Usage

wp_nav_menu( $args );

Given a theme_location parameter, the function displays the menu assigned to that location. If no such location exists or no menu is assigned to it, the parameter fallback_cb will determine what is displayed.

If not given a theme_location parameter, the function displays

  • the menu matching the ID, slug, or name given by the menu parameter; e.g. wp_nav_menu( 'menu=123' ) or wp_nav_menu( [ 'menu' => 'My Menu' ] )

  • otherwise, the first non-empty menu;

  • otherwise (or if the menu given by menu is empty), output of the function given by the fallback_cb parameter (wp_page_menu(), by default);

  • otherwise, nothing.

(Note: I slightly modified the above quote.)

So be sure to check the reference/docs and supply the proper function arguments. And remember that the above $args can be an array like array( 'key' => 'value' ) (or [ 'key' => 'value' ]) or a query string like key=value&key2=value+2. Both will work in most cases, but when the value contains a space or the value is complex (e.g. it contains HTML tags), then you should use the array format. :)

发布评论

评论列表(0)

  1. 暂无评论