Thank you for your reply.
I did come across that thread earlier, but the answers didn’t seem to work for me.
Anyway, I used the groups_get_user_groups()
function you mentioned in your reply and came up with a solution doing what I was looking for along with comma separation of the group names:
<?php
$group_ids = groups_get_user_groups(bp_displayed_user_id());
foreach($group_ids["groups"] as $group_id) {
echo(groups_get_group(array( 'group_id' => $group_id )) -> name . (end($group_ids["groups"]) == $group_id ? '' : ', ' ) );
}
?>
Just in case anybody else needs this too, I have included an updated version of the code in form of a function. It now displays links to the groups a user is a member of and it’s possible to toggle display of hidden groups.
// display links to the groups a user is member of
function user_group_memberships( $user_id = null, $show_hidden = false ) {
$group_ids = groups_get_user_groups($user_id);
$visible_group_ids = array();
foreach($group_ids["groups"] as $group_id) {
if (!$show_hidden) {
if(groups_get_group(array( 'group_id' => $group_id )) -> status !== 'hidden') {
$visible_group_ids[] = $group_id;
}
} else {
$visible_group_ids[] = $group_id;
}
}
if (empty($visible_group_ids)) {
echo 'None';
} else {
foreach($visible_group_ids as $visible_group_id) {
echo(
'<a title="View group page" href="' . home_url() . '/groups/' . groups_get_group(array( 'group_id' => $visible_group_id )) -> slug . '">' .
groups_get_group(array( 'group_id' => $visible_group_id )) -> name . '</a>' .
(end($visible_group_ids) == $visible_group_id ? '' : ', ' )
);
}
}
}
This is my first ever PHP function so any feedback is welcome.
how did you implement that into (I guess) a page? Did you use a shortcode?