仅允许游客浏览wordpress指定分类的文章
- 时间:2020-05-17 12:16:18
- 分类:网络文摘
- 阅读:102 次
经常玩论坛的朋友都知道,在通过 Discuz、Phpwind 等源码搭建的论坛中,可以设置游客的访问权限,限制游客浏览某些版块。其实在 开放式的 wordpress 中我们同样可以设置只允许游客浏览指定分类的文章。方法如下:
添加如下代码到当前 wordpress主题的函数模版 functions.php 文件中。
- // 首页和指定分类文章可以访问
- add_action( 'template_redirect', 'ashuwp_show_only_login', 0 );
- function ashuwp_show_only_login(){
- //判断登录,只允许访问ID为3和2的分类文章
- if( !in_category( array( 3,2 ) ) && !is_home() && !is_user_logged_in() ){
- auth_redirect(); //跳转到登录页面
- exit();
- }
- }
默认未登录者只允许访问网站首页及分类ID为3和2的分类归档页面和文章,否则跳转到登录页面。
把 !in_category 前面的感叹号去掉改成 in_category,则正好相反,访问分类ID为3和2的分类文章跳转到登录,其它文章可以正常访问。
代码出自:https://zmingcx.com/wordpress-only-allows-viewing-of-specific-posts.html
推荐阅读:5 Ways to Use Your Smartphone to Build a Better Blog The Most Expensive Domain Sales Ever 15 Ways Of How Not To Kill Your Leadership Authority Study Shows Strong Growth of Tech Inventions to Fight Climate Ch Interested in Bitcoins? Here are 10 Blogs You Need to Check Out Your Blog’s Been Hacked! Here’s What You Need to Do #DiningForBrussels: How Belgium Is Fighting Terrorism With A For Many WordPress Site Hackings Caused To These Plugins GoDaddy Company Now Hosts New Enterprise-Level Plans Using Reduce to Count the Array in Javascript
- 评论列表
-
- 添加评论