Adding Menu Support to Custom Wordpress Theme

2019-08-06 09:58发布

I am creating my first Wordpress theme and I am struggling with menu support:

I added a custom menu in functions.php and implemented it into my header.php like shown below but the menu-option in the administration area does not show up!

# functions.php

<?php

    add_theme_support( 'menus' );

    add_action( 'init', 'register_my_menus' );

    function register_my_menus() {
        register_nav_menus(
            array(
                'primary-menu' => __( 'Primary Menu' ),
                'secondary-menu' => __( 'Secondary Menu' )
            )
        );
    }

?>

# header.php
# [...]
    <?php wp_nav_menu( array( 'theme_location' => 'primary-menu' ) ); ?>
# [...]

My Setting:

  • Wordpress Version 3.4.2
  • MAMP Development Environment
  • No plugins

Other information:

  • The menu option shows up in other templates
  • The menu is getting rendered correctly on the page

What am I missing here?


Edit #1

I can't even see the menu option in the admin menu (like here!)

1条回答
混吃等死
2楼-- · 2019-08-06 10:37

Few things - You don't need add_theme_support(); nor the add_action('init', 'register_my_menus')

Just straight up call the register_nav_menus function, like so:

register_nav_menus(
    array(
    'primary-menu' => __( 'Primary Menu' ),
    'secondary-menu' => __( 'Secondary Menu' )
    )
);

Can also check if the function exists if you desire. But if it's only for use on your own theme and you know it exists it's not really needed.

if ( function_exists( 'register_nav_menus' ) ) {
    ...
}
查看更多
登录 后发表回答