仅允许游客浏览wordpress指定分类的文章

  • 时间:2020-05-17 12:16:18
  • 分类:网络文摘
  • 阅读:157 次

经常玩论坛的朋友都知道,在通过 Discuz、Phpwind 等源码搭建的论坛中,可以设置游客的访问权限,限制游客浏览某些版块。其实在 开放式的 wordpress 中我们同样可以设置只允许游客浏览指定分类的文章。方法如下:

仅允许游客浏览wordpress指定分类的文章

添加如下代码到当前 wordpress主题的函数模版 functions.php 文件中。

  1. // 首页和指定分类文章可以访问
  2. add_action( 'template_redirect', 'ashuwp_show_only_login', 0 );
  3. function ashuwp_show_only_login(){
  4.     //判断登录,只允许访问ID为3和2的分类文章
  5.     if( !in_category( array( 3,2 ) ) && !is_home() && !is_user_logged_in() ){
  6.         auth_redirect(); //跳转到登录页面
  7.         exit();
  8.     }
  9. }

默认未登录者只允许访问网站首页及分类ID为3和2的分类归档页面和文章,否则跳转到登录页面。

把 !in_category 前面的感叹号去掉改成 in_category,则正好相反,访问分类ID为3和2的分类文章跳转到登录,其它文章可以正常访问。

代码出自:https://zmingcx.com/wordpress-only-allows-viewing-of-specific-posts.html

推荐阅读:
Young Cancer Patient Has Decided To Live-Blog His Health’s Decli  How a CDN can be your Blog’s Secret Weapon  Why Wasted Words Kill Good Ideas And Cost Sales  Why You Need Niche Experience to Be a Great Blogger  The Art of the Perfect Listicle  Tools and Resources to Help Create Your Next Content Calendar  5 Tips for Capturing More Leads on Your Blog  Don’t Fall Victim to the Content Overproduction Trap  Are You Taking Advantage Of These Free WordPress Marketing Plugi  What Content Marketers Can Learn from Game of Thrones 
评论列表
添加评论