Skip to:
Content
Pages
Categories
Search
Top
Bottom

Activity Custom Content After Post Iteration

  • @tommyhares

    Participant

    Hello,

    Currently trying to work with the bp_after_activity_entry() to place the latest blog posts below every fifth activity item. However, since the function works like a loop, I cannot add to the iteration. I’m trying to place blog posts from the latest to the last throguhout the Activity Feed, every fifth slot. I would rather not use JS for this, so if there is another function that is able to do this, I would appreciate it. Thank you!

Viewing 11 replies - 1 through 11 (of 11 total)
  • @henrywright

    Moderator

    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

    @tommyhares

    Participant

    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?

    @henrywright

    Moderator

    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

    @tommyhares

    Participant
    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; ?>

    @tommyhares

    Participant

    I assume I have to use the JS ajaxComplete() and run an increment to pass back to the php

    @henrywright

    Moderator

    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.

    @tommyhares

    Participant

    Thanks again for your help Henry. I tried your additions, however the counter still gets reset to 0 after the Load More gets initiated

    @henrywright

    Moderator

    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.

    @tommyhares

    Participant

    How do I do that on the server side? Or did you mean client-side?

    @henrywright

    Moderator

    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?

    @tommyhares

    Participant

    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.

Viewing 11 replies - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.
Skip to toolbar