在functions.php中注册菜单。
首先要注册菜单,将以下函数添加至function.php函数里 。
参考2020主题的functions.php中,注册菜单代码:
/**
* Register navigation menus uses wp_nav_menu in five places.
*/
function twentytwenty_menus() {
$locations = array(
'primary' => __( 'Desktop Horizontal Menu', 'twentytwenty' ),
'expanded' => __( 'Desktop Expanded Menu', 'twentytwenty' ),
'mobile' => __( 'Mobile Menu', 'twentytwenty' ),
'footer' => __( 'Footer Menu', 'twentytwenty' ),
'social' => __( 'Social Menu', 'twentytwenty' ),
);
register_nav_menus( $locations );
}
add_action( 'init', 'twentytwenty_menus' );
左边是别名,右边是名称。别名会用在导航栏的调用上,名称则显示在菜单后台页面上。
通过add_action()添加到响应钩子里:
1、“init” 钩子 ;
2、“ after_setup_theme ”;
3、或参考2019主题,把上面的代码放在twentynineteen_setup()函数,并通过add_action()添加到”after_setup_theme”钩子里。