你只需要get_terms()
让我们写wp_menu_id_by_name( $name )
/**
* Gets a menu id by name
*
* @param string $name The menu name
* @return int|boolean The menu id or false if not found
*/
function wp_menu_id_by_name( $name ) {
$menus = get_terms( \'nav_menu\' );
foreach ( $menus as $menu ) {
if( $name === $menu->name ) {
return $menu->term_id;
}
}
return false;
}
让我们使用它
echo \'My Special Menu id is \' . wp_menu_id_by_name( \'My Special menu\' );
// Outputs: My Special Menu id is 3