0

I am attempting to create a listing of Blog Posts by Date(Month) and show the featured image for each.

                <div id="datePostTime"><h2>Posts for <?php echo get_the_date('F, Y'); ?></h2></div>
                <?php // The loop ?>
                <?php if ( have_posts() ) {
                    while ( have_posts() ) {
                        if ( has_post_thumbnail() ) {
                            echo "<div id='post_thumbnail'>";
                                the_post_thumbnail();
                                echo '<h4>' . get_post(get_post_thumbnail_id())->post_excerpt . '</h4>';
                            echo "</div>";
                        }
                        the_post();
                        the_content();
                        echo "<br />";
                        echo "<div id='the_post_date'>";
                            the_date('','Article posted on ', '.');
                        echo "</div>";
                        echo "<br />";
                    } // end while
                } // end if ?>

My code above is showing each individual blog post. However, the very first thumbnail image is then posted for each blog post. So regardless of what thumbnail I pick for the post, all of them show the same image as the first one loaded.

So it looks like this -

> Image 1
> My Post
> Post date
> Image 1 again
> My next post
> next post date

Why am I having repeating thumbnail images?

1 Answer 1

0

This is wrong

<?php if ( have_posts() ) {
                    while ( have_posts() ) {
                        if ( has_post_thumbnail() ) {
                            echo "<div id='post_thumbnail'>";
                                the_post_thumbnail();
                                echo '<h4>' . get_post(get_post_thumbnail_id())->post_excerpt . '</h4>';
                            echo "</div>";
                        }
                        the_post();

You are calling your thumbnail before the_post(). Just move the_post() to before if ( has_post_thumbnail() ) {

1
  • This fixed it. Thank you. Sometimes it just takes an extra pair of eyes. Commented Aug 4, 2014 at 16:00

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.