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

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

经常玩论坛的朋友都知道,在通过 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

推荐阅读:
How to Convert Float Number (Fraction) to Hexadecimal in Python?  4 Tips You Shouldn’t Miss While Creating Your Ideal Websit  SQL Algorithm to Compute Shortest Distance in a Plane  How to Count Univalue Subtrees in a Binary Tree?  Have Writer’s Block? Try These Content Ideation Tools for Market  Quick Ways to Relax Once You’ve Hit Publish  How to Prepare Your SEO Strategy for the Internet of Things  Keep Yourself Safe: Social Media Strategies  7 Ways to Use Video on Your Blog to Get More Engagement  Should You Build Your Blog on a CMS or a Website Builder? 
评论列表
添加评论