If you use this code directly below while ( bp_members() ) : bp_the_member();
in the members loop, you’ll be able to see which members are editors.
<?php
$user_id = bp_get_member_user_id();
$user = new WP_User( $user_id );
if ( $user->roles[0] == 'editor' ) {
echo 'this user is an editor';
} else {
echo 'this user is not an editor';
}
?>
I know this isn’t exactly what you’d like to do but it should put you on the right track.
Note: This code assumes that all of your users have a single role assigned.
Thanks for your help!
So that will still output all members, but just add a note saying which ones are editors, correct? I would still like to have however a /members page that works right out of the box displaying all members. Just would like a separate page showing only editors.
<?php
$user_id = bp_get_member_user_id();
$user = new WP_User( $user_id );
if ( $user->roles[0] == 'editor' )
break;
?>
If you add that code directly below while ( bp_members() ) : bp_the_member();
you’ll get a list of members who are not editors.
<?php
$user_id = bp_get_member_user_id();
$user = new WP_User( $user_id );
if ( $user->roles[0] != 'editor' )
break;
?>
That’ll display only editors
Thank you henry! Now how can I implement that into my site? Do I have to create a new php file add the code that will display editors to create a new page that outputs only editors?
It is easily done. You’d need to create a new page template:
https://codex.wordpress.org/Page_Templates
You can then use a modified members loop inside the page template you create:
Members Loop
Hi henry thanks for all of your help.
I think I have the code somewhat working, at least it says up top viewing members 1 of 5 (I have 1 member assigned as editor). But it doesn’t pull anything like their avatar and name etc.
http://postimg.org/image/ay8fbd23f/
Here is my code (the part that you recommended)
<ul id="members-list" class="item-list" role="main">
<?php while ( bp_members() ) : bp_the_member(); ?>
<?php
$user_id = bp_get_member_user_id();
$user = new WP_User( $user_id );
if ( $user->roles[0] != 'editor' )
break;
?>
<li>
<div class="item-avatar">
<a href="<?php bp_member_permalink(); ?>"><?php bp_member_avatar(); ?></a>
</div>
<div class="item">
<div class="item-title">
<a href="<?php bp_member_permalink(); ?>"><?php bp_member_name(); ?></a>
<?php if ( bp_get_member_latest_update() ) : ?>
<span class="update"> <?php bp_member_latest_update(); ?></span>
<?php endif; ?>
</div>
<div class="item-meta"><span class="activity"><?php bp_member_last_active(); ?></span></div>
<?php do_action( 'bp_directory_members_item' ); ?>
<?php
/***
* If you want to show specific profile fields here you can,
* but it'll add an extra query for each member in the loop
* (only one regardless of the number of fields you show):
*
* bp_member_profile_data( 'field=the field name' );
*/
?>
</div>
<div class="action">
<?php do_action( 'bp_directory_members_actions' ); ?>
</div>
<div class="clear"></div>
</li>
<?php endwhile; ?>
Change break; to continue;
Brilliant! Thanks so much!
Hi everyone,
Thanks for all of your help! Now my next question is, is it possible to display all members with the role greater than editor? I’m not a php coder (as you can tell)
But could we modify this snippet?
if ( $user->roles[0] != 'editor' )
To say greater than editor?
This is very helpful, but the counts for active members on the Member Directory page are still incorrect. I copied the members-loop.php into a buddypress/members directory in my child theme and added the code where you described, but obviously the count for “All Members” and “Viewing x of x members” are coming from somewhere else.