仅允许游客浏览wordpress指定分类的文章
- 时间:2020-05-17 12:16:18
- 分类:网络文摘
- 阅读:163 次
经常玩论坛的朋友都知道,在通过 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
推荐阅读:How to Fix CloudFlare Error 1101 (Worker threw exception)? Python Function to Convert Excel Sheet Column Titles to Numbers Algorithm to Find the Kth Missing Positive Number in Array How to Partition a String into Palindromes using DFS Algorithm? How to Get Blockchain Version of Steem RPC Node using Javascript How to Find All Duplicates in an Array using Python? Bruteforce and Rolling Hash Algorithm to Compute the Longest Hap How to Choose the Right Products and Services to Blog About Buying a Home as a Blogger 5 Social Media Blogs to Know in 2020
- 评论列表
-
- 添加评论