WordPress -> 如果角色是订阅者,是否显示图像?

5
所以这是个问题..我把Wordpress和bbPress与会员软件(aMember)集成了起来。
在我的bbPress论坛上,我想要在每个成员的用户名下显示他们的WordPress角色(而不是bbpress角色),并根据每个成员的角色显示一个图像。
例如,
如果用户角色是订阅者->在bbpress中的用户名下显示角色->还要显示下面的图像。
我想显示WordPress角色(而不是bbpress角色)的原因是我的会员软件(amember)允许我根据用户的订阅设置不同的WordPress角色。 我的网站上有2个不同的会员计划(一个免费和一个付费),我想根据他们的计划在我的bbpress论坛中显示不同的图像。
我查看了bbPress模板,并在loop-single-reply.php中找到了以下代码:
<?php bbp_reply_author_link( array( 'sep' => '<br />', 'show_role' => true ) ); ?> // this shows the bbpress role
<?php echo 'Points: '.cp_getPoints(bbp_get_reply_author_id()); ?> // this shows the member points below the username - I use a points plugin)

现在我该如何用一段代码替换这段代码,以显示每个用户的WordPress角色(而不是bbpress),并根据其角色显示下面的图像。例如:
如果是“订阅者”角色->显示角色+图像
如果是“撰稿人”角色->显示角色+图像
如果是“管理员”角色->显示角色+图像
我不是程序员,所以不知道如何完成这个任务。请帮忙。我找到了一些相关的代码,我认为我可以用它来实现这个目标:
<?php if ( current_user_can('contributor') ) : ?>
Content
<?php endif; ?>

现在我的失败尝试看起来像这样:
<?php 

$user_roles = $current_user->roles;
$current_user = $bbp_get_reply_author_id; // i think this is wrong :P
$user_role = array_shift($user_roles);
?>
<?php if ($user_role == 'administrator') : ?>

Show Role  
Show Image

<?php elseif ($user_role == 'editor') : ?>

Show Role
Show Editor Image

<?php elseif ($user_role == 'author') : ?>

Show Role
Show Author Image

<?php elseif ($user_role == 'contributor') : ?>

Show Role
Show Contributor Image

<?php elseif ($user_role == 'subscriber') : ?>

Show Role
Show Subscriber Image

<?php else : ?> 

Show Role

<?php endif ?>

我完全不知道自己在做什么……上面的代码是我在谷歌上找到的。

有人可以帮忙吗?

非常感激。

3个回答

14

如果要测试用户是否为订阅者,代码可能类似于这样。

<?php
global $current_user; 
get_currentuserinfo(); 
if ( user_can( $current_user, "subscriber" ) ){ 
// Show Role
// Show Subscriber Image
} 

如果您想进行多个用户检查,我建议使用switch语句,如下所示。

global $current_user; 
get_currentuserinfo();
switch (true)  {
 case ( user_can( $current_user, "subscriber") ):
   // Show Role
   // Show Subscriber Image
 break;
 case ( user_can( $current_user, "contributor") ):
   // Show Role
   // Show Contributor Image
 break;
 case ( user_can( $current_user, "administrator") ):
   // Show Role
   // Show Administrator Image
 break;
}

你可以继续使用switch语句来处理更多的用户角色。

已编辑

global $current_user; 
get_currentuserinfo();
switch (true)  {
 case ( user_can( $current_user, "subscriber") ):
   echo '<img src="http:www.impho.com/images/001.jpg">';
 break;
 case ( user_can( $current_user, "contributor") ):
   echo '<img src="http:www.impho.com/images/002.jpg">';
 break;
 case ( user_can( $current_user, "administrator") ):
  echo '<img src="http:www.impho.com/images/003.jpg">';
 break;
}

根据用户要求编辑

好的,这样做就可以了,使用以下代码替换您已有的获取用户名、头像、积分和图像的所有代码。

在您的函数中添加以下代码:

function userLooping($role, $img)
{
$user_query = new WP_User_Query( array( 'role' => $role ) );

// User Loop
if ( !empty( $user_query->results ) ):
    foreach ( $user_query->results as $user ):
        echo '<p>'. $user->display_name.'</p>'; // display user name
        echo '<p>'.get_avatar($user->ID).'</p>'; // display user avatar
        //echo '<p>Points: '.cp_getPoints(bbp_get_reply_author_id()).'</p>';
        echo '<p>'.$img.'</p>'; //display image based on role
    endforeach;
endif;
}

在上面的echo前面删除//

将以下内容放入您的模板中

<?php 
$args = array( array('role' => 'administrator', 'img' => '<img src="http://placeape.com/100/100">'),array('role' => 'subscriber', 'img' => '<img src="http://placekitten.com/100/100">'));
foreach ($args as $arg):
 userLooping($arg['role'],$arg['img']);
endforeach;
?>

要添加更多的角色和图片,只需在订阅者数组后面添加一个新数组即可。


抱歉,再试一次。我的 switch 语句中有一个多余的括号,我已经编辑了代码并重新发布了。 - David Chase
还是不行...我得到了这个错误:解析错误:语法错误,意外的T_BREAK... - Allen Payne
是的,所有用户都在WordPress上注册。我用你的代码再试了一次,但是一直出现这个错误:Parse error: syntax error, unexpected '<'... 这样看起来对吗?http://img818.imageshack.us/img818/3599/image2mfx.jpg - Allen Payne
请查看编辑后的答案,我已经调整好了,以便在任何时候都可以正常工作 :) - David Chase
@AllenPayne 非常感谢!很高兴一切都为您顺利解决 :) - David Chase
显示剩余19条评论

1
我发现了一个简单得多的解决方案:
<?php
global $forumuser; 
$forumuser = bbp_get_reply_author_id();
switch (true)  {
case ( user_can( $forumuser, "author") ):
echo '<img src="http://www.impho.com/images/image1.png">';
break;
case ( user_can( $forumuser, "premium") ):
echo '<img src="http://www.impho.com/images/image2.png">';
break;
case ( user_can( $forumuser, "starter") ):
echo '<img src="http://www.impho.com/images/image3.png">';
break;
case ( user_can( $forumuser, "subscriber") ):
echo '<img src="http://www.impho.com/images/image4.png">';
break;
}
?>

“starter”和“premium”角色是我使用“用户角色编辑器”插件创建的自定义角色。
现在一切似乎都正常工作。

1

我知道可能有点晚了,但是这是我认为最短、最有效的解决方案:

$current_user = new WP_User(wp_get_current_user()->id);
$user_roles = $current_user->roles; 
foreach ($user_roles as $role) {

   if  ($role == 'subscriber' ){
     //code here for subscribers
   }

   if  ($role == 'editor' ){
     //code here for editors
   }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接