总的来说,这应该不是什么大问题。菜单的构建速度可能很慢,因此您最好的选择是使用transient缓存菜单。
if ( !get_transient( \'first_menu_transient\' ) ) {
ob_start(); // do not directly output the menu
// build the menu
$first_menu = ob_get_contents();
ob_end_clean();
echo $first_menu;
set_transient( \'first_menu_transient\', $first_menu );
} else {
echo get_transient( \'first_menu_transient\' );
}
通过这种方式,与每次构建整个菜单相比,您可以将数据库查询减少到最小。
为避免更改和保存菜单后出现错误的菜单,请删除wp_update_nav_menu
行动
add_action(\'wp_update_nav_menu\', \'my_delete_menu_transients\');
function my_delete_menu_transients($nav_menu_selected_id) {
delete_transient( \'first_menu_transient\' ); // you should also just delete the transient of the updated menu, but you get my point - you would have to write the function for linking the menu-IDs to your transient names. For example, just put the ID of the menu in the transient name.
}
目前一切都清楚了吗?