0

I've created a custom post type with ACF custom fields included. The CPT is being placed inside of a bootstrap slider. Everything seems to be working, except, the posts being rendered on the page are being multiplied.

There are only 4 different posts should be rendered on the page, but each one is rendering (and I counted them) 47 times. I echoed the post ID just be sure it was the same post. I can confirm that it is. I can't figure out why. Any insight or help would be appreciated.

Here is my code:

               <div id="carouselTestimonial" class="carousel slide" data-bs-ride="carousel">
                    <div class="carousel-inner">
                        <?php 
                        $cptQuery = new WP_Query($args = [  
                            'post_type' => 'client-testimonial',
                            'post_status' => 'publish',
                            'posts_per_page' => -1
                        ]);
                        $active = 'active';

                        while ( $cptQuery->have_posts() ) : $cptQuery->the_post(); 
                            setup_postdata( $cptQuery );
                            $cptID = get_the_ID();
                            $addTestHomePage = get_field('homepage_testimonial_carousel');
                            $testComanyName = get_field('client_testimonial_company_name');
                            $testFirstName = get_field('client_testimonial_client_first_name');
                            $testLastName = get_field('client_testimonial_client_last_name');
                            $testTitle = get_field('client_testimonial_client_title');
                            $testImage = get_field('client_testimonial_client_picture');
                            if( $testImage ):
                                $testImgurl = $testImage['url'];
                                $testImgtitle = $testImage['title'];
                                $testImgalt = $testImage['alt'];
                                $testImgcaption = $testImage['caption'];
                            endif;
                            $testAltImage = get_field('client_testimonial_secondary_image');
                            $testQuote = get_field('client_testimonial_quote');

                            if ($addTestHomePage === "yes") : 

                                foreach($cptQuery as $cptPost) :
                                    ?>
                                    <div class="carousel-item <?php echo $active;?>">
                                        <?php echo $cptID; ?>
                                        <div class="row">
                                            <div class="col-3 col-md-3 col-sm-3">
                                                <div class="imagesDiv">
                                                    <?php if ($testImage) { ?>
                                                        <img class="images" src="<?php echo $testImgurl; ?>" title="<?php echo $testImgtitle; ?>" alt="<?php echo $testImgalt; ?>">
                                                    <?php } else { ?>
                                                        <img class="images" src="<?php echo get_home_url(); ?>/wp-content/uploads/2023/08/<?php echo $testAltImage; ?>.webp" title="Stock Image of a Profile Picture" alt="This is a stock photo">
                                                    <?php } ?>
                                                </div>
                                            </div>
                                            <div class="col-9 col-md-3 col-sm-9">
                                                <?php if ($testQuote) : ?>
                                                    <div class="largeQuote"></div>
                                                    <div class="testimonialQuote">
                                                        <?php echo $testQuote; ?>
                                                    </div>
                                                    <div class="byline"><?php echo $testFirstName; ?> <?php echo $testLastName; ?>, <?php echo $testTitle; ?></div><br />
                                                    <div class="byline"><?php echo $testComanyName; ?></div>
                                                <?php endif; ?>                                        
                                            </div>
                                        </div>
                                    </div>
                                    <?php
                                    $active = '';
                                wp_reset_postdata();
                                endforeach;
                            endif; //homepage carousel check
                        endwhile;
                        //wp_reset_postdata(); ?>
                    </div>
                </div>
                <a class="carousel-control-prev" href="#carouselTestimonial" role="button" data-bs-slide="prev">
                    <span class="carousel-control-prev-icon" aria-hidden="true">Prev</span>
                    <span class="sr-only">Previous</span>
                </a>
                <a class="carousel-control-next" href="#carouselTestimonial" role="button" data-bs-slide="next">
                    <span class="carousel-control-next-icon" aria-hidden="true">Next</span>
                    <span class="sr-only">Next</span>
                </a>

I've attached a screenshot of the posts in the CPT list: screenshot of posts in WP admin

And a screenshot of the source code from the rendered page: source code of the page when rendered

1 Answer 1

0

Unless I've mis-interpreted what you are trying to accomplish, you added the foreach loop and you don't need it. Having it run while inside a while loop seems to be duplicating your intentions. Remove the foreach loop altogether, delete the first wp_reset_postdata();, and un-comment the 2nd one:

<div id="carouselTestimonial" class="carousel slide" data-bs-ride="carousel">
    <div class="carousel-inner">
        <?php 
        $cptQuery = new WP_Query($args = [  
            'post_type' => 'client-testimonial',
            'post_status' => 'publish',
            'posts_per_page' => -1
        ]);
        $active = 'active';

        while ( $cptQuery->have_posts() ) : $cptQuery->the_post(); 
            setup_postdata( $cptQuery );
            $cptID = get_the_ID();
            $addTestHomePage = get_field('homepage_testimonial_carousel');
            $testComanyName = get_field('client_testimonial_company_name');
            $testFirstName = get_field('client_testimonial_client_first_name');
            $testLastName = get_field('client_testimonial_client_last_name');
            $testTitle = get_field('client_testimonial_client_title');
            $testImage = get_field('client_testimonial_client_picture');
            if( $testImage ):
                $testImgurl = $testImage['url'];
                $testImgtitle = $testImage['title'];
                $testImgalt = $testImage['alt'];
                $testImgcaption = $testImage['caption'];
            endif;
            $testAltImage = get_field('client_testimonial_secondary_image');
            $testQuote = get_field('client_testimonial_quote');

            if ($addTestHomePage === "yes") : 
                ?>
                <div class="carousel-item <?php echo $active;?>">
                    <?php echo $cptID; ?>
                    <div class="row">
                        <div class="col-3 col-md-3 col-sm-3">
                            <div class="imagesDiv">
                                <?php if ($testImage) { ?>
                                    <img class="images" src="<?php echo $testImgurl; ?>" title="<?php echo $testImgtitle; ?>" alt="<?php echo $testImgalt; ?>">
                                <?php } else { ?>
                                    <img class="images" src="<?php echo get_home_url(); ?>/wp-content/uploads/2023/08/<?php echo $testAltImage; ?>.webp" title="Stock Image of a Profile Picture" alt="This is a stock photo">
                                <?php } ?>
                            </div>
                        </div>
                        <div class="col-9 col-md-3 col-sm-9">
                            <?php if ($testQuote) : ?>
                                <div class="largeQuote"></div>
                                <div class="testimonialQuote">
                                    <?php echo $testQuote; ?>
                                </div>
                                <div class="byline"><?php echo $testFirstName; ?> <?php echo $testLastName; ?>, <?php echo $testTitle; ?></div><br />
                                <div class="byline"><?php echo $testComanyName; ?></div>
                            <?php endif; ?>                                        
                        </div>
                    </div>
                </div>
                <?php
                $active = '';
            endif; //homepage carousel check
        endwhile;
        wp_reset_postdata(); ?>
    </div>
</div>
<a class="carousel-control-prev" href="#carouselTestimonial" role="button" data-bs-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true">Prev</span>
    <span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselTestimonial" role="button" data-bs-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true">Next</span>
    <span class="sr-only">Next</span>
</a>
1
  • Thank you @rudtek. That corrected the issue. Very much appreciated.
    – Mitch S
    Commented Sep 4, 2023 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.