wordpress内容对特定对象显示

LMS
709 0

据说wordpress对特定对象有如下等级区分的:

  1. 管理员 Administrator: level 10
  2. 编辑 Editor: Level 7
  3. 作者 Author: Level 4
  4. 投稿者 Contributor: Level 2
  5. 订阅者 Subscriber: Level 0

上面的分级是wordpress2.5版本内的默认分级,只有注册登陆后的才有这种分级,普通访客属于0以下级别的,不知道现在3.0还有没有。

有了分级就可以根据针对不通级别显示内容了,比如管理员可见的:

<?php global $user_ID; if( $user_ID ) : ?>
<?php if( current_user_can('level_10') ) : ?>
[管理员可见内容]
<?php endif; ?>
<?php endif; ?>

也可以简单写成这样:

<?php if($GLOBALS['user_login']=='admin') { echo '[管理员可见内容]'; } ?>

对于不同级别的不同显示则可以通过下面的PHP判断来实现:

<?php if (current_user_can('level_10')) : ?>
<?php print "管理员可见内容"; ?>
<?php elseif (current_user_can('level_7')) : ?>
<?php print "编辑可见内容"; ?>
<?php elseif (current_user_can('level_4')) : ?>
<?php print "作者可见内容"; ?>
<?php elseif (current_user_can('level_2')) : ?>
<?php print "投稿者可见内容"; ?>
<?php elseif (current_user_can('level_0')) : ?>
<?php print "订阅者可见内容"; ?>
<?php else : ?>
<?php print "对不起,你没有登陆"; ?>
<?php endif; ?>

当然,如果只是针对是否登陆,则可以简单的写成:

<?php if (is_user_logged_in()){
echo "欢迎你,登陆者";
}
else {
echo "欢迎你,来访者";
};
?>

上面说的功能都是直接写在主题模板里的,写主题模板的时候已经写死,除此之外还可以通过wordpress短代码来实现对登录和非登陆访客显示或隐藏内容。

首先,去functions.php加入下列代码:

add_shortcode( 'member', 'member_check_shortcode' );
function member_check_shortcode( $atts, $content = null ) {
if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
return $content;
return '';
}

然后在发表文章时,在HTML模式下输入[member]针对非登陆访客隐藏的内容[/member]就可以了。

THE END

发表评论

Submit