epetersen
epetersen

Reputation: 3

How to add div to the last post and every 5th post

The loop it is currently set to add a 'review' every 5th project post, I would like it to do this and add a 'review' after the last post. For example, if there are 13 posts it adds a 'review' after the 5th, 10th, and 13th post.

I have tried doing a post count and adding that as an 'or' statement, however, that just tries to add a 'review' after every post.

<?php
/**
 * Template Name: Work
 *
 * This template display content at full with, with no sidebars.
 * Please note that this is the WordPress construct of pages and that other 'pages' on your WordPress site will use a different template.
 *
 * @package some_like_it_neat
 */

get_header(); ?>


<div class="content-area">
    <section class="work not-home work-content page-content ">
        <div class="row page-header">
            <h1><?php echo get_field('page_title', get_the_ID()); ?></h1>
            <div class="work-page-content">
                <?php echo get_field('page_content', get_the_ID()); ?>
            </div>
        </div>

        <div class="project-section">
            <div class="row">
                <?php 
                $posts = get_posts();
                $count = count($posts); 

                $args = array(
                    'post_type' => 'work',
                    'posts_per_page' => -1
                );
                $q = new WP_Query( $args );

                $posts_iterator = 0; //set to last to display first icon
                $icon_iterator = 0;
                $is_right = true;
                $review_iterator = 0;



                $icons = get_field('icons', get_the_ID());
                $reviews = get_field('reviews', get_the_ID());

                $true_reviews = get_sub_field('rave_review', get_the_ID());

                ?>

                <?php if ( $q->have_posts() ): ?>
                <div class="works">
                    <?php while ( $q->have_posts() ): $q->the_post(); ?>
                        <div class="featured-work">
                            <?php if($posts_iterator == 0): ?>
                                <div style="text-align: <?php echo $is_right ? 'right' : 'left' ?>;">
                                    <img class="work-icon" src="<?php echo $icons[$icon_iterator]['icon_image'] ?>" />
                                </div>
                            <?php 
                                $icon_iterator++;
                                if ( $is_right ) {
                                    $is_right = false;
                                } else {
                                    $is_right = true;
                                }
                                if( $icon_iterator == count($icons)) {
                                    $icon_iterator = 0;
                                }

                                endif; 
                            ?>
                            <a class="lazy featured-work__link arrow-hover" href="<?php the_permalink(); ?>">
                                <div class="project-panel">
                                    <div class="featured-work-img">
                                        <img src="<?php the_field('home_image'); ?>">
                                    </div>
                                    <div class="featured-work-spacer">
                                        &nbsp;
                                    </div>
                                    <div class="featured-work-text match">
                                        <div class="featured-work-text-inner">
                                            <p class="categories"><?php the_field('category'); ?></p>
                                            <h3 class="project-name link-arrow"><?php the_title(); ?></h3>
                                        </div>
                                    </div>
                                </div>

                            </a>
                        </div>




                        <?php if($posts_iterator == 4 && $review_iterator != count($reviews) ) : ?>
                            <div class="rave-review work-content__text-wrap">
                                <div class="review-inner">
                                    <p><?php echo $reviews[$review_iterator]['reviewer_copy'] ?></p>
                                </div>
                                <div class="reviewer-info">
                                    <p>
                                        <span class="reviewer-name"><?php echo $reviews[$review_iterator]['reviewer_name'] ?></span>
                                        <span class="reviewer-title"><?php echo $reviews[$review_iterator]['reviewer_title'] ?></span>
                                        <span class="reviewer-company"><?php echo $reviews[$review_iterator]['reviewer_company'] ?></span>
                                    </p>
                                </div>
                            </div>
                        <?php 
                            $review_iterator++;
                            endif;
                        ?>



                    <?php
                        if( $posts_iterator == 4 ) {
                            $posts_iterator = 0;

                        } else {
                            $posts_iterator++; 

                        }
                        endwhile;
                    ?>

                </div>
                <?php endif; ?>
            </div>
        </div>
    </section>


</div><!-- #primary -->

<?php get_footer(); ?>

Upvotes: 0

Views: 64

Answers (1)

Solrac
Solrac

Reputation: 931

There are a number of ways to achieve this but basically you want to use modulo at the end of your loop to avoid duplicates.

Let's say that you have a loop with a counter and that loop does 10, 15 or 20 iterations. At the end of the loop (outside of your loop) you want to check that counter:

echo $counter % 5 === 0 ? '' : $lastReview;

If the remainder of $counter divided by 5 equals 0 you php won't echo a review, otherwise, it will.

Upvotes: 2

Related Questions