首页 > CMS建站 > Wordpress > 正文

WordPress设置只有注册用户才能浏览特定的内容

2020-06-10 12:58:11
字体:
来源:转载
供稿:网友

WordPress有些时间我们需要一些特色功能,下面我来介绍WordPress设置只有注册用户才能浏览特定的内容,有需要的朋友可学习学习.

我们增加一个自定义字段:user_only,如果这个值不为零,这这篇日志或者页面是只能给注册用户浏览,然后通过 the_content 来控制内容显示,这样就能简单的并且灵活设置具体到哪篇文章或者页面是只能注册用户浏览,详细代码如下:

  1. <?php 
  2. add_filter('the_content''post_user_only'); 
  3. function post_user_only($text){ 
  4.     global $post
  5.      
  6.     $user_only = get_post_meta($post->ID, 'user_only', true); 
  7.     if($user_only){ 
  8.         global $user_ID
  9.         if(!$user_ID){ 
  10.             $redirect = get_permalink($post->ID); 
  11.             $text = '该内容仅限于会员浏览,请<a href="'.wp_login_url($redirect).'">登录</a>!';//开源软件:Cuoxin.com 
  12.         } 
  13.     } 
  14.     return $text
  15. ?> 

把上面带复制成一个文件上传到插件目录,激活即可.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选