当前位置:首页 > CMS教程 > WordPress > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2015-01-21 22:08:06 浏览: 评论:0 

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>!';//开源软件:phpfensi.com 
  12.         } 
  13.     } 
  14.     return $text
  15. ?> 

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

Tags: WordPress用户用户 WordPress内容

分享到: