我以编程方式创建自己的菜单:
$menu_id = wp_create_nav_menu($menu_name);
wp_update_nav_menu_item($menu_id, 0, array(
\'menu-item-title\' => __(\'Home\'),
\'menu-item-classes\' => \'home\',
\'menu-item-url\' => home_url( \'/\' ),
\'menu-item-status\' => \'publish\'));
wp_update_nav_menu_item($menu_id, 0, array(
\'menu-item-title\' => __(\'Custom Page\'),
\'menu-item-url\' => home_url( \'/custom/\' ),
\'menu-item-status\' => \'publish\'));
wp_update_nav_menu_item($menu_id, 0, array(
\'menu-item-title\' => __(\'SHIT\'),
\'menu-item-url\' => home_url( \'/custom/\' ),
\'menu-item-status\' => \'publish\'));
现在我可以用现有的菜单替换我的菜单了。我应该调用什么代码?感谢Peter
最合适的回答,由SO网友:Frank P. Walentynowicz 整理而成
将此代码添加到functions.php
您当前的主题(最好是子主题):
$menuName = \'Your menu name\';
$locationID = \'primary\';
$myMenu = get_term_by(\'name\', $menuName, \'nav_menu\');
$locations = get_theme_mod(\'nav_menu_locations\');
if($myMenu->term_id !== $locations[$locationID]) {
$locations[$locationID] = $myMenu->term_id;
set_theme_mod(\'nav_menu_locations\', $locations);
}
记住更改前两个变量的值以符合您的要求。