drgawfsf1069 2015-06-02 06:10
浏览 48
已采纳

Wordpress - 如何在wp_nav_menu中显示“私人”页面的链接?

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?

  • 写回答

3条回答 默认 最新

  • dongquanlin1885 2015-06-02 06:22
    关注

    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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?