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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器