Wordpress - how to show links to 'private' pages in wp_nav_menu?

蓝咒 提交于 2020-07-23 09:15:20

问题


In my WordPress site I have a requirement to show private links in the main navigation (via wp_nav_menu) even if the user isn't logged in (I just need to show the link, not change who can view the actual content).

I can do this elsewhere, using wp_list_pages and specifying the post_status(es) I want to display but I can't see how to apply that to wp_nav_menu.

This works:

wp_list_pages(array(
    'title_li'      => '',
    'child_of'      => $page->ID,    
    'post_status'   => 'published,private'
));

Is there a way to do something similar with this?

wp_nav_menu(array(
    'menu'              => 'primary',
    'theme_location'    => 'primary',
    'container'         => FALSE,
    'walker'            => new MegaMenuWalker
    'depth'             => 2));

My searches so far show a bunch of ways to post-process the results to filter out private pages (or drafts) using the wp_get_nav_menu_items hook but I have yet to turn up something on doing the reverse.

I suppose I could do a custom query to grab items that way but that wouldn't let me use my custom walker.

Tantalisingly there's wp_get_nav_menu_items( $menu, $args ) which does allow me to specify the post_status argument, however I'm left to apply my own render rather than being able to use the walker... unless there's some way to use the walker outside the context of wp_nav_menu?


回答1:


The wp_nav_menu function only walks through the links in the desired menu and it's not fetching them. As can be seen in the source code (line 270) the function that fetch the links get only the name/id/etc. of the menu and not any other arguments.

From the manual:

Displays a navigation menu created in the Appearance → Menus panel.

Given a theme_location parameter, the function displays the menu assigned to that location. If no such location exists or no menu is assigned to it, the parameter fallback_cb will determine what is displayed.

If not given a theme_location parameter, the function displays

  • the menu matching the ID, slug, or name given by the menu parameter;
  • otherwise, the first non-empty menu; otherwise (or if the menu given by menu is empty), output of the function given by the fallback_cb parameter (wp_page_menu(), by default);
  • otherwise nothing.

Therefore, you need to add those privates pages to the menu using the WP admin panel or create a function of your own that fetches the pages and build a menu (please notice: not a nav_walker as it just use the data and doesn't fetch it).




回答2:


Here is a short snippet to show Private Pages in the menu builder :

add_filter( 'nav_menu_meta_box_object', 'show_private_pages_menu_selection' );
/**
* Add query argument for selecting pages to add to a menu
*/
function show_private_pages_menu_selection( $args ){
    if( $args->name == 'page' ) {
        $args->_default_query['post_status'] = array('publish','private');
    }
    return $args;
}

Add this to a custom plugin or to your theme functions.php, and you will be able to add private page into any menu.




回答3:


Building off of the accepted answer, Private pages don't show up in the menu builder by default. Here's a way to get around that issue.



来源:https://stackoverflow.com/questions/30588611/wordpress-how-to-show-links-to-private-pages-in-wp-nav-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!