首先,您的代码是错误的。不允许您将if
内部声明echo
.
然而,如果您使用默认WordPress菜单(外观-菜单)创建菜单,这绝对是正确的方法,而不是硬编码,更好的方法是根据您的条件使用wp_nav_menu_objects
滤器
add_filter(\'wp_nav_menu_objects\', \'remove_menu_items\', 10, 2);
function remove_menu_items($removed_menu_objects, $args) {
// if current user role is Editor or Administrator, do not remove anything
if( current_user_can(\'editor\') || current_user_can(\'administrator\') )
return $removed_menu_objects;
// for any other user role, remove menu items with title of "Logout" and "Contact"
$remove = array(\'Logout\', \'Contact\');
foreach ($removed_menu_objects as $key => $menu_object) {
if (in_array($menu_object->title, $remove)) {
unset($removed_menu_objects[$key]);
}
}
return $removed_menu_objects;
}
代码进入函数内部。活动子主题的php。您还可以比较菜单项ID,而不是它们的标题。