Print out images from custom field wordpress

I have a custom field called images, I am attempting to loop through my custom post and for each post print out 3 images. I’ve started them in an array but whenever I try to print the contents of the area I get weird output, like the images duplicating. I’ve attempted a simple while loop but instead of limiting it to 3 it prints out every image attached.

<?php $i=0; while ($page_query->have_posts()): $page_query->the_post(); ?> 
        <section class="featured-block">
            <div class="container">
                <div class="row">
                    <div class="col-md-12">
                        <button class="title-button center-block"><?php echo get_the_title(); ?></button>
                    </div>
                    <!-- end of col md 12 -->
                </div>
                <div class="row home-img-row">
                    <div class="col-md-4 <?php print the_ID(); ?>">
                    <?php $meta_values = get_post_meta( $post->ID, 'images');
                    $i = 0; ?>
                    <? while ($i < 3) {
                        print($meta_values[$i]);
                        $i++;
                    }?>

Related posts

Leave a Reply

1 comment

  1. It might help to debug your $meta_values variable. Also, keep your PHP open tags consistent (<?php is highly preferable). You would also probably want to do a foreach loop on an array, vs a while loop.

    <?php
    $meta_values = get_post_meta( $post->ID, 'images');
    var_dump($meta_values); // Debug
    foreach ($meta_values as $value) {
        echo $value;
    }
    ?>