wordpress 菜单_如何在WordPress中的菜单中添加条件逻辑

wordpress 菜单

Do you want to show different navigation menu items for different pages in WordPress? Perhaps showing a different menu to logged in users, or hiding a menu item on particular pages? Normally it would require you to add code snippets or use CSS to hide menu items, but wouldn’t it be great if you could do that inside your WordPress admin area? In this article, we will show you how to add conditional logic to menus in WordPress.

是否要为WordPress中的不同页面显示不同的导航菜单项? 也许显示与登录用户不同的菜单,或者在特定页面上隐藏菜单项? 通常,这需要您添加代码段或使用CSS隐藏菜单项,但是如果您可以在WordPress管理区域内执行此操作,那不是很好吗? 在本文中,我们将向您展示如何在WordPress中向菜单添加条件逻辑。

影片教学 (Video Tutorial)

演示地址

If you don’t like the video or need more instructions, then continue reading.
First thing you need to do is install and activate the If Menu plugin. It works out of the box. All you need to do is visit Appearance » Menus and start editing your navigation menus.

如果您不喜欢该视频或需要更多说明,请继续阅读。
您需要做的第一件事是安装并激活If Menu插件。 开箱即用。 您所需要做的就是访问外观»菜单,然后开始编辑导航菜单。

Start by selecting an item in the navigation menu and click the ‘Enable Conditional Logic’ checkbox. This will show the conditional menu, which looks like this:

首先在导航菜单中选择一个项目,然后单击“启用条件逻辑”复选框。 这将显示条件菜单,如下所示:

Conditional menu

Next, choose whether you want to show or hide that item and then choose the condition. For example, only show the logout page link when a user is logged in.

接下来,选择要显示还是隐藏该项目,然后选择条件。 例如,仅在用户登录时显示注销页面链接。

After choosing a condition, simply press the save menu button to store your changes.

选择条件后,只需按保存菜单按钮即可存储您的更改。

添加自己的条件 (Adding Your Own Conditions)

If Menu is a new plugin, and there are not many conditions that you can apply. WordPress provides many more conditional tags that you can use in your projects. For a full list check out this page.

如果Menu是一个新插件,则可以应用的条件并不多。 WordPress提供了更多可在项目中使用的条件标签。 有关完整列表,请查看此页面

You can add any of these conditional tags to be used with the If Menu plugin. For example, let’s assume that you want to show/hide a menu item on custom post type pages.

您可以添加任何这些条件标签以与If Menu插件一起使用。 例如,假设您要显示/隐藏自定义帖子类型页面上的菜单项。

You would add the code snippet to your theme’s functions.php file or a site-specific plugin like this.

您可以将代码段添加到主题的functions.php文件或类似站点的特定插件中


add_filter( 'if_menu_conditions', 'wpb_new_menu_conditions' );

function wpb_new_menu_conditions( $conditions ) {
  $conditions[] = array(
    'name'    =>  'If it is Custom Post Type archive', // name of the condition
    'condition' =>  function($item) {          // callback - must return TRUE or FALSE
      return is_post_type_archive();
    }
  );

  return $conditions;
}

This is how it would appear in the conditional logic menu.

这就是它出现在条件逻辑菜单中的方式。

Adding your own conditions in the conditional menus

That’s all. We hope this article helped you add conditional logic to menus in WordPress. You may also want to check out our guide on how to style WordPress navigation menus.

就这样。 我们希望本文能帮助您向WordPress中的菜单添加条件逻辑。 您可能还需要查看有关如何设置WordPress导航菜单样式的指南。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Google+.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在TwitterGoogle+上找到我们。

翻译自: https://www.wpbeginner.com/plugins/how-to-add-conditional-logic-to-menus-in-wordpress/

wordpress 菜单

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值