4

所以这是交易..我将 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 ?>

我不知道我在做什么......上面的代码是我在谷歌上找到的。

任何人都可以帮忙吗?

我真的很感激。

4

3 回答 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;
?>

要添加更多角色和图像,只需在订阅者之后添加一个新数组

于 2013-03-11T01:44:11.013 回答
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;
}
?>

初学者和高级角色是我使用用户角色编辑器插件创建的自定义角色。

现在一切似乎都运行良好。

于 2013-03-11T16:34:39.943 回答
0

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

$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
   }

}
于 2014-07-29T13:40:33.550 回答