我相信有一个更好的解决方案,适用于菜单真正动态的情况(例如,它们应该根据用户角色而改变,或者如果菜单项的组合太多,需要在运行时确定)。
Wordpress\'wp_nav_menu_objects
挂钩最方便:
add_filter(\'wp_nav_menu_objects\', \'addMenuItems\', null, 2);
function addMenuItems($sortedItems, $args)
{
$toAdd = [
(object) [
\'ID\' => 1, // make sure this doesn\'t conflict with existing items
\'db_id\' => 1, // same here
\'title\' => \'To Google!\',
\'url\' => \'https://www.google.com\',
],
];
return array_merge($sortedItems, $toAdd);
}