Multiple activity loops on same page
Is it possible to run multiple activity loops independently on one page? i.e. if one changes the filtering on one, it doesn’t affect the other.
Essentially, I’m looking for the BuddyPress equivalent of creating a new wp_query object (WordPress).
Anyone have an example of setting it up?
Can I just write something along the lines of:
$myloop = new bp_activity_template(my args);
$myloop2 = new bp_activity_template(my args 2);
while ($myloop->bp_activities()) : $myloop->bp_the_activity();
while ($myloop2->bp_activities()) : $myloop2->bp_the_activity();
How are you calling your activity loops? (the bp_has_activity calls, please)
And when you say “changes the filtering”, do you mean via PHP or an AJAX dropdown on the page?
Oh, you just posted some as I was writing. As far as I’m aware this should work.
the only problem you might have is the ajax cookies used to define the request of the activity filtering – if you have multiple activities and the filter options, a conflict might occur with the returned data.
I have a mini activity sidebar widget that’s permanently around showing all site activity.
When a user is on a profile activity page, there is also the main activity stream. Essentially, I want them to be able to move between friends, groups, personal in the main activity stream, without it affecting the sidebar widget.
I’ll play around with my suggested code (now I have your additional votes of confidence), and see how it runs.
I’ll tackle the AJAX conflicts if and when they arise.
I ran into a stack of problems with this approach:
1. A call to make a new BP_Activity_Template requires a full set of parameters (it doesn’t default them).
2. All the bp_ functions, e.g. bp_has_activities() work on a single global activities template ($activities_template)
3. The methods (functions) you can call directly on the BP_Activity_Template calls are very limited (has_activities, next_activity, rewind_activities, user_activities, the_activity).
[Edit: the call to the_activity() references a global $activity but then doesn’t do anything with it, e.g. it doesn’t instantiate it]
4. All the presentational / data functions you’d want to use, e.g. for pagination_count, title, avatar, item ids, object names, content, only work on the global $activity_template (not on the $activity global)
Am I missing something obvious? Or is this really not feasible with the current methods?
The code I tried:
$page = 1; $per_page = 10; $scope = ‘ ‘, $max = 200;
$activities = new BP_Activity_Template ($page, $per_page, $max, false, ‘DESC’, false, false, false, false);
if ($activities->has_activities()) :
while ( $activities->user_activities() ) : $activities->the_activity();
then, I was stuck as there don’t seem to be any easy methods to access the activity content.
My surmise is that the_activity() should be instantiating the global $activity (but it doesn’t), and then the bp_ functions should work on the $activity global not on the $activity_template global
Some of the function’s parameters are what I call “smart”; depending on what type of object it controls, and who is logged in and what part of the site is being viewed, if arguments are specified, it will put in some defaults. Obviously if you have a loop that’s changing content from page-to-page that you don’t want, you need to pass an argument appropriately.
There’s no way of creating a second BP Activity Stream template loop object, for example, like you can with wp_query. As long as you don’t try to call an Activity Stream within an Activity Stream loop, I think it will be fine (with passing arguments per first paragraph).
Look in bp-activity-templatetags.php for functions for the Activity Stream template loop; I can’t quickly find a list in the codex.
missed your first reference – why not just call bp_has_activities() sequentially on the page?- all the templatetags reference the global $activities_template
@djpaul – I put some edits in my comment above – it looks like our posts crossed.
My two streams are independent (i.e. not nested)… I’ll try setting them both up with a full set of parameters specified, and see if that allows them to show different content.
@nuprn1 – yes, they were running sequentially, but the second one was just returning the same activities as the first.
As @djpaul suggests, I’ll make sure it’s getting a fuller set of parameters passed in its bp_has_activities() initiation, to keep it showing all activities. Hopefully, this should fix the PHP call.
[Edit: and there doesn’t seem to be any problem with the AJAX – e.g. changing the main drop down selector to just show blog posts, only affects the main activity stream, and doesn’t interfere with the second stream.]
What would a full set of parameters look like?
I’m struggling with this an pagination. When I click page 2 of one loop, the other jumps to page 2 as well. Is this an issue of me relying on “smart” parameters instead of adding them independently? Or is this something else?
They are similar loops, but with different sets of users contributing.
You can see the loops here:
@gregfielding – Hi! I’m running into similar problems. I defined a seperate loop (for announcements, more important than other updtaes) to be displayed on the top of the page. But when I add an update in the ‘normal’ loop, the same update is also added to the custom loop on top, even though it doesn’t match the filter set to that loop.
Did you manage to solve your problem? Is it a case of making sure all IDs and Classes are different, so AJAX call doesn’t reach the custom loop? Any help or pointers would be highly appreciated!
Nope. Still stuck.
Anyone know if there are any changes to this in BP 1.5? (before pulling out more hair trying to solve this now…)
Where is ‘bp_dtheme_ajax_querystring_activity_filter’?
My buddypress default theme doesn’t have such function
- The topic ‘Multiple activity loops on same page’ is closed to new replies.