[WordPress] How to hide private pages in wp_nav_menu
![[WordPress] How to hide private pages in wp_nav_menu](img/ogp.png)
Introducing a filter hook to hide private pages in menus created with wp_nav_menu() in WordPress.
What I want to do
When a static page is set to private

This part of the menu also becomes "private", but

It still appears in the actual menu.(Even when logged out of WordPress)

Therefore,
Add the following code to functions.php.
//メニューから非公開のページは削除
function hide_private_pages_from_menu ($items, $args) {
foreach ($items as $ix => $obj) {
if (!is_user_logged_in () && 'private' == get_post_status ($obj->object_id)) {
unset ($items[$ix]);
}
}
return $items;
}
add_filter ('wp_nav_menu_objects', 'hide_private_pages_from_menu', 10, 2);
Then, it disappeared from the menu.(Please check while logged out of WordPress)





If this was helpful, we appreciate your support!
Your support will be used for childcare.
Or support us by buying something from the buttons below
(You don't have to buy the linked product.)
Amazon
楽天市場
Yahoo!ショッピング
PR