admin管理员组文章数量:1122832
Scenario I have three pages namely John James and Jessica. The Jessica page ID is 666 and I want to filter it out so that it DOES NOT show in the menu. I need help with a simple code similar to the one pasted below, that will help me achieve that. I have deliberately not registered any menu location in my function.php file.
function remove_jessica_page(){
#
wp_nav_menu(
array(
'exclude'=> 666 // exclude Jessica page from menu
)
);
}
apply_filters('wp_nav_menu_items','remove_thankyou_page');
Scenario I have three pages namely John James and Jessica. The Jessica page ID is 666 and I want to filter it out so that it DOES NOT show in the menu. I need help with a simple code similar to the one pasted below, that will help me achieve that. I have deliberately not registered any menu location in my function.php file.
function remove_jessica_page(){
#
wp_nav_menu(
array(
'exclude'=> 666 // exclude Jessica page from menu
)
);
}
apply_filters('wp_nav_menu_items','remove_thankyou_page');
Share
Improve this question
edited Jun 28, 2024 at 10:22
Tom J Nowell♦
60.7k7 gold badges77 silver badges147 bronze badges
asked Jun 28, 2024 at 10:00
Nippon VisionsNippon Visions
111 bronze badge
3
- 1 normally this is done manually, but you mentioned you aren't registering nav menus deliberately, what's the context/reasoning behind this? I ask because some of the answers might not work for you. – Tom J Nowell ♦ Commented Jun 28, 2024 at 10:22
- I intend to use the concept in plug-making, and Different themes that may use the plugin will have different registered nav menus and locations. Is my approach wrong? – Nippon Visions Commented Jul 3, 2024 at 13:47
- hmm that's good to know, though if your plugin is creating the post and you know the slug it's using, then CSS could hide the menu item and you wouldn't need PHP to remove the HTML – Tom J Nowell ♦ Commented Jul 3, 2024 at 14:20
1 Answer
Reset to default 1That's not the filter to use, instead use the wp_nav_menu_objects
filter documented at: https://developer.wordpress.org/reference/hooks/wp_nav_menu_objects/
The docs contain a helpful code example provided by a contributor at the bottom, which I've modified slightly:
function wpse_unset_menu_items( $menu_objects, $args ) {
// remove this if you want it on all menus not just a specific menu aka primary_menu:
if ( 'primary_menu' !== $args->theme_location ) {
return $menu_objects;
}
// if the user is logged in, don't do anything.
if ( is_user_logged_in() ) {
return $menu_objects;
}
// these is the list of things to hide.
$items_to_hide = array(
'Cart',
'Wishlist',
);
foreach ( $menu_objects as $key => $menu_object ) {
if ( ! in_array( $menu_object->title, $items_to_hide ) ) {
continue;
}
unset( $menu_objects[ $key ] );
}
return $menu_objects;
}
add_filter( 'wp_nav_menu_objects', 'wpse_unset_menu_items', 10, 2 );
Modifying this to check something other than the title or to check for Jessica
should be straightfoward
Magic Numbers
Avoid hardcoding post IDs such as 666
! If the page gets deleted by accident it'll reappear in the menu as the ID will change when its recreated. It also means the code can only be used on 1 site, and even a migration to another host could break it!
本文标签:
版权声明:本文标题:In WordPress, I want to use code to filter out one menu item (page) from displaying when I call wp_nav_menu() function 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736301892a1931335.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论