Skip to:
Content
Pages
Categories
Search
Top
Bottom

Activity Custom Content After Post Iteration


  • tommyhares
    Participant

    @tommyhares

    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)

  • Henry Wright
    Moderator

    @henrywright

    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

    @tommyhares

    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?


    Henry Wright
    Moderator

    @henrywright

    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

    @tommyhares

    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

    @tommyhares

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


    Henry Wright
    Moderator

    @henrywright

    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

    @tommyhares

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


    Henry Wright
    Moderator

    @henrywright

    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

    @tommyhares

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


    Henry Wright
    Moderator

    @henrywright

    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

    @tommyhares

    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