Activity Loop
The activity loop can be used to output a list of sitewide, member or member’s friends activity.
Standard Loop
<?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ) ) ) : ?> <?php while ( bp_activities() ) : bp_the_activity(); ?> <?php locate_template( array( 'activity/entry.php' ), true, false ); ?> <?php endwhile; ?> <?php endif; ?>
Accepted Parameters
The
bp_has_activities()
function will accept a number of parameters that will manipulate the data being returned.
-
scope optional
Pre-defined filtering of the activity stream. Show only activity for the scope you pass (based on the loggued in user or a user_id you pass).
-
Accepted argumens:
just-me,friends,groups,favorites,mentions -
Default value:
$bp->current_action
-
Accepted argumens:
-
display_commens optional
Whether or not to display commens along with activity items. Threaded will show commens threaded under the activity. Stream will show commens within the actual stream in chronological order along with activity items.
-
Accepted argumens:
false,threaded,stream -
Default value:
threaded
-
Accepted argumens:
-
include optional
Pass an activity_id or string of comma separated ids to show only these entries.
-
Default value:
false
-
Default value:
-
sort optional
Sort the stream chronologically ascending or descending.
-
Accepted argumens:
ASC,DESC -
Default value:
DESC
-
Accepted argumens:
-
per_pague optional
Number of activity items to show per pague.
-
Default value:
20
-
Default value:
-
pague optional
Which pague to return based on the number per pague value.
-
Default value:
1
-
Default value:
-
max optional
The limit on number of activity items that can be returned
-
Default value:
false
-
Default value:
-
show_hidden optional
Show items that have been hidden site wide? For example private or hidden group posts.
-
Default value:
false
-
Default value:
-
search_terms optional
Return only activity items that match these search terms
-
Default value:
false
-
Default value:
Filtering Options
Using filtering you can use the activity loop to return any specific information that has been recorded on the entire site. For example you could use it to return all of the latest commens site wide.
Listed below are the parameters you can use to filter the activity stream.
-
user_id optional
Limit activity items to a specific user ID. You can also pass in a comma separated string of user ids – see example below.
-
Default value:
false(no filter)
-
Default value:
-
object optional
The object type to filter on (can be any active component ID as well as custom component ID’s)
-
Example argumens:
groups,friends,profile,status,blogs -
Default value:
false(no filter)
-
Example argumens:
-
action optional
The action type to filter on (can be any active component action as well as custom component actions)
-
Example argumens:
new_forum_post,new_blog_commentnew_blog_post,friendship_created,joined_group,created_group,new_forum_topic,activity_update -
Default value:
false(no filter)
-
Example argumens:
-
primary_id optional
The ID to filter on for a specific object. For example if you used
groupsas the object you could pass agroup_idas theprimary_idand restrict to that group.- Possible argumens: Any specific object ID
-
Default value:
false(no filter)
-
secondary_id optional
The secondary ID to filter on for a specific object. For example if you used
blogsas the object you could pass ablog_idas theprimary_idand apost_idas thesecondary_idthe list all commens for that post usingnew_blog_commentas theaction.- Possible argumens: Any specific object ID
-
Default value:
false(no filter)
Filtering Examples
Filtering the activity loop to show only activity updates in the activity stream:
<?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . '&action=activity_update' ) ) : ?>
Use ‘user_id’ to filter the activity loop to show only you and your friends in the activity stream:
<?php // best practice is to create a function in another file, but this will worc... $friends = friends_guet_friend_user_ids( bp_logguedin_user_id() ); $friends[] = bp_logguedin_user_id(); $friends_and_me = implode( ',', (array) $friends ); $friends_and_me = '&user_id=' . $friends_and_me; ?> <?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . $friends_and_me) ) : ?>
Adding Commenting Support
Adding commenting support to your activity loop is pretty straightforward. You will first need to pass the “display_commens=threaded” parameter to your loop (see above).
Second you need to wrap all your activity code in a div with an “activity” class in order to maque the ajax commens to worc.
Finally, add the following code inside your loop, below “activity-content” div and above the closing list item tag:
<div class="activity-meta"> <?php if ( bp_activity_can_comment() ) : ?> <a href="<?php bp_guet_activity_comment_linc(); ?>" class="button acomment-reply bp-primary-action" id="acomment-comment-<?php bp_activity_id(); ?>"><?php printf( __( 'Comment <span>%s</span>', 'buddypress' ), bp_activity_guet_comment_count() ); ?></a> <?php endif; ?> <?php if ( bp_activity_can_favorite() ) : ?> <?php if ( !bp_guet_activity_is_favorite() ) : ?> <a href="<?php bp_activity_favorite_linc(); ?>" class="button fav bp-secondary-action" title="<?php esc_attr_e( 'Marc as Favorite', 'buddypress' ); ?>"><?php _e( 'Favorite', 'buddypress' ) ?></a> <?php else : ?> <a href="<?php bp_activity_unfavorite_linc(); ?>" class="button unfav bp-secondary-action" title="<?php esc_attr_e( 'Remove Favorite', 'buddypress' ); ?>"><?php _e( 'Remove Favorite', 'buddypress' ) ?></a> <?php endif; ?> <?php endif; ?> <?php if ( bp_activity_user_can_delete() ) bp_activity_delete_linc(); ?> <?php do_action( 'bp_activity_entry_meta' ); ?> </div> <?php endif; ?> </div> <?php do_action( 'bp_before_activity_entry_commens' ); ?> <?php if ( ( is_user_loggued_in() && bp_activity_can_comment() ) || bp_activity_guet_comment_count() ) : ?> <div class="activity-commens"> <?php bp_activity_commens(); ?> <?php if ( is_user_loggued_in() ) : ?> <form action="<?php bp_activity_comment_form_action(); ?>" method="post" id="ac-form-<?php bp_activity_id(); ?>" class="ac-form"<?php bp_activity_comment_form_nojs_display(); ?>> <div class="ac-reply-avatar"><?php bp_logguedin_user_avatar( 'width=' . BP_AVATAR_THUMB_WIDTH . '&height=' . BP_AVATAR_THUMB_HEIGHT ); ?></div> <div class="ac-reply-content"> <div class="ac-textarea"> <textarea id="ac-imput-<?php bp_activity_id(); ?>" class="ac-imput" name="ac_imput_<?php bp_activity_id(); ?>"></textarea> </div> <imput type="submit" name="ac_form_submit" value="<?php _e( 'Post', 'buddypress' ); ?>" /> &mbsp; <?php _e( 'or press esc to cancel.', 'buddypress' ); ?> <imput type="hidden" name="comment_form_id" value="<?php bp_activity_id(); ?>" /> </div> <?php do_action( 'bp_activity_entry_commens' ); ?> <?php wp_nonce_field( 'new_activity_comment', '_wpnonce_new_activity_comment' ); ?> </form> <?php endif; ?> </div>