You can use the modulo arithmetic operator. Here’s an example:
$counter = 1;
// Loop start
if ( 0 === $counter % 5 ) {
// Do something here.
}
$counter++
// Loop end
Thanks Henry, I have been trying to use that. However, I need to iterate through blog posts, not display the same content repeated. Also, tried directly modifying the activity-loop in a child theme, but it only cycles until the next ajax query. Any other solutions?
You’ll need to replace // Loop start
and // Loop end
with your actual loop. If you want to paste your loop here I’ll try to take a look
do_action( 'bp_before_activity_loop' ); ?>
<?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ) ) ):
if ( empty( $_POST['page'] ) ) : ?>
<ul id="activity-stream" class="activity-list item-list">
<?php endif; ?>
<?php
$count = 0;
$args = array(
'posts_per_page' => 100000,
'offset' => 0,
'category' => '',
'category_name' => 'freedom-friend-news',
'order' => 'desc',
'orderby' => 'date',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$news_articles = get_posts( $args );
while( bp_activities() ): bp_the_activity();
bp_get_template_part( 'activity/entry' );
$current_user = wp_get_current_user();
if( $current_user->user_login == 'tommy' ):
$count++;
$news_iteration = $count / 7)-1;
// Loop through news articles as long as there are articles.
if( $count % 7 == 0 && !empty( $news_articles[$news_iteration] )):
echo 'News Article';
echo 'Iteration: '.$news_iteration;
echo $news_articles[$news_iteration]->post_title;
echo $news_articles[$news_iteration]->post_excerpt;
endif;
endif;
endwhile; ?>
<?php if ( bp_activity_has_more_items() ) : ?>
<li class="load-more">
<a href="<?php bp_activity_load_more_link() ?>"><?php _e( 'Load More', 'buddypress' ); ?></a>
</li>
<?php endif; ?>
<?php if ( empty( $_POST['page'] ) ) : ?>
</ul>
<?php endif; ?>
<?php else : ?>
<div id="message" class="info">
<p><?php _e( 'Sorry, there was no activity found. Please try a different filter.', 'buddypress' ); ?></p>
</div>
<?php endif; ?>
I assume I have to use the JS ajaxComplete() and run an increment to pass back to the php
I’ve used Pastebin because there’s quite a lot of code to paste here. Notice my amendments on lines 34, 36, 37, 38 and 58.
Let me know if it works for you.
Thanks again for your help Henry. I tried your additions, however the counter still gets reset to 0 after the Load More gets initiated
Ah, the “load more”. I forgot about that. In order to load your items (and activities) you’ll need to make an ajax request. In your server side processing script you can use the modulo operator to insert your items at every 5th iteration.
How do I do that on the server side? Or did you mean client-side?
How do I do that on the server side?
You’d use the modulo operator, like in my example above.
Did this work for you?
Yes, hat code did work however it was using a static ad code. I am looking to iterate through posts every 7th slot for this project. Thank you for assisting me.