我建议您使用wp_nav_menu_objects
钩子,它允许您将菜单项作为对象而不是字符串来使用。在if语句中,可以创建任意数量的项
add_filter( \'wp_nav_menu_objects\', \'example_last_nav_item\', 10, 2 );
function example_last_nav_item( $items, $args ){
if ( $args->theme_location==\'account-menu\' && is_user_logged_in() ){
//parent menu item
$parent_item = array(
\'title\' => __(\'Profile\'),
\'menu_item_parent\' => 0, //PARENT ELEMENT HAS 0
\'ID\' => \'profile\',
\'db_id\' => 9999, //MAKE SURE IT\'s UNIQUE ID
\'url\' => \'http://google.com\',
\'classes\' => array( \'custom-menu-item\' )
);
//submenu menu item
$submenu_item = array(
\'title\' => __(\'Submenu first element\'),
\'menu_item_parent\' => 9999, //FOR SUBMENU ITEM SET PARENT\'s db_id
\'ID\' => \'submenu-item\',
\'db_id\' => 99992,
\'url\' => \'http://google.com\',
\'classes\' => array( \'custom-menu-item\' )
);
$items[] = (object) $parent_item;
$items[] = (object) $submenu_item;
}
return $items;
}
关于您可以阅读的所有项目属性
here