Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP code causing ACF php code below it to disappear

I’m not sure if there is an error in my syntax, but for some reason if I insert the following PHP code, then it causes the ACF field below it <?php the_field('fieldname'); ?> to disappear from the content entirely (like not even appear in the html code). If I paste it below the ACF field it’s fine, only if it is above does this issue occur.

<?php
        $args = array( 'post_type' => 'post', 'numberposts' => -1);
        $lastposts = get_posts( $args );
        foreach ( $lastposts as $post ): setup_postdata( $post );
        ?>
        <div class="col">
        <p class="ingress">&ldquo;<?php the_field('review'); ?>&rdquo;</p>
        <img src="<?php the_field('star_rating'); ?>" class="star-rating"><br>
        <img src="<?php the_field('image'); ?>" class="avatar">
        <p class="name-location"><?php the_field('name_location'); ?></p>
        </div>
        <?php endforeach; ?>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

setup_postdata() modifies the global variables for the $post. This sets the post to a different one in the loop when you run setup_postdata(). After your endforeach; you should have wp_reset_postdata(); to put the global variables back to where they were before your foreach loop.

See the docs for more information on setup_postdata

Here is their description of what happens which is probably better worded than mine.

setup_postdata() does not assign the global $post variable so it’s important that you do this yourself. Failure to do so will cause problems with any hooks that use any of the above globals in conjunction with the $post global, as they will refer to separate entities.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading