Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] How to create Loop from groups_get_user_groups output


  • rzelnik
    Participant

    @rzelnik

    I am trying to create a widget that would show the list of groups that a particular user is a member of. I have created this:

    <?php
    $user_id = get_current_user_id();
    $user_groups = (groups_get_user_groups($user_id));
    $user_groups = $user_groups["groups"];
    ?>
    <ul class="my-groups-list" class="item-list">
    <?php foreach ($user_groups as $group): ?>
    	<li><?php echo $group ?></li>
    <?php endforeach; ?>
    </ul>

    This code displays a list of the user’s group ID’s. Now I would like to create a loop from this query, so that I could display the group permalinks and names instead of the ID, like this:

    <ul class="my-groups-list" class="item-list">
    	<?php while ( ... something() ) : bp_the_group(); ?>
    		<li><a href="<?php bp_group_permalink() ?>"><?php bp_group_name() ?></a></li>
    	<?php endwhile; ?>
    </ul>

    How can I do that?

    I use WordPress 4.0 and BuddyPress 2.0.2.

    Thanks for your help.

Viewing 3 replies - 1 through 3 (of 3 total)

  • Roger Coathup
    Participant

    @rogercoathup

    For a loop like that you can use bp_has_groups() instead of groups_get_user_groups(). Is there a reason why you rejected that approach?

    Then a my groups widget can have a simple core like this:

    
    		<?php if ( bp_has_groups( 'type=alphabetical&user_id=' . bp_loggedin_user_id() ) ) : ?>
    
    			<ul id="my-groups-list">
    				<?php while ( bp_groups() ) : bp_the_group(); ?>
    					<li class="row collapse">
    						<div class="item-avatar small-3 columns">
    							<a href="<?php bp_group_permalink() ?>"><?php bp_group_avatar_thumb() ?></a>
    						</div>
    
    						<div class="item small-9 columns">
    							<div class="item-title"><a href="<?php bp_group_permalink() ?>" title="<?php bp_group_name() ?>"><?php bp_group_name() ?></a></div>
    						</div>
    					</li>
    				<?php endwhile; ?>
    			</ul>
    
    		<?php else: ?>
    
    			<div class="widget-error"><?php _e( 'No current groups.', 'my-groups-widget' ); ?></div>
    
    		<?php endif; ?>
    

    Note: If you want to use functions like bp_group_name(), you need to be inside a bp_has_groups() loop. The loop and bp_the_group() instantiate structures in the group template that are then used by bp_group_name.

    If you want to use groups_get_user_groups() and setup your own loop, you’ll need to use different functions to access the name, permalink, etc. Look in bp-groups-functions.php, bp-groups-template.php and in bp-group-classes.php to get an idea what’s available.


    Roger Coathup
    Participant

    @rogercoathup

    Correction — you can also use bp_group_name() by passing a group to it as its argument.

    Take your group_id, instantiate a group, and pass it to bp_group_name():

    
    $args = array( 
      'group_id' => $my_id
    );
    
    $group = groups_get_group( $args );
    ..
    bp_group_name( $group);
    

    rzelnik
    Participant

    @rzelnik

    Thank you Roger, both versions work well.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] How to create Loop from groups_get_user_groups output’ is closed to new replies.
Skip to toolbar