Dyon
Dyon

Reputation: 195

has_post_thumbnail() not working on pages

im reusing some code i used on the index.php in my wordpress theme. Everything works, except of the thumbnails (which work on index.php). I can display them. But if i try to check via (has_post_thumbnail()) it always returns false. Where could be the problem?

The problem is in the following part:

if ( has_post_thumbnail() ) {
                        $photo = get_the_post_thumbnail( $recent['ID'], 'large' );
                        echo  '<div class="section1-2singlephoto">' . $photo . '</div>';
                    }

                    else {
                        echo 'no Thumbnail';
                    }

Only using the following code would work:

                    $photo = get_the_post_thumbnail( $recent['ID'], 'large' );
                    echo  '<div class="section1-2singlephoto">' . 

Can anybody give me a hint why this might not be working. I made a page a page template and added the code into it. Here is the full code:

 <?php
            $args = array( 'numberposts' => '20',
                'offset'    => '1'
            );
            $recent_posts = wp_get_recent_posts( $args );
            foreach( $recent_posts as $recent ){




                echo '<div class="section1-2single large-12 medium-12 small-12 column">';

                echo '<a class="" href="' . get_permalink($recent["ID"]) . '"><h4>' . $recent["post_title"] .'</h4></a>';


                echo '<div class="large-12 medium-12 small-12 row">';

                echo '<div class="large-2 medium-2 small-2 columns">';

                // GET COMMENTS NUMBER
                $comments_count = wp_count_comments( $recent["ID"] );
                echo '<div class=metaDataContainer>';
                if ($comments_count->total_comments === 0) {
                    echo '<i class="fa fa-comment-o commentsNumbre"> 0</i>';
                };
                if ($comments_count->total_comments === 1) {
                    echo '<i class="fa fa-comment-o commentsNumbre"> '. $comments_count->total_comments .'</i>';
                };
                if ($comments_count->total_comments > 1) {
                    echo '<i class="fa fa-comment-o commentsNumbre"> '. $comments_count->total_comments .'</i>';
                };

                echo do_shortcode('[mashshare text="'. $recent["post_title"] .'" url="'. get_permalink($recent["ID"]) .' shares="false" align="center" "]');

                echo '</div>';
                echo '</div>';


                echo '<div class="large-10 medium-10 small-10 columns">';
// GET THE THUMBNAIL
                if ( has_post_thumbnail() ) {
                    $photo = get_the_post_thumbnail( $recent['ID'], 'large' );
                    echo  '<div class="section1-2singlephoto">' . $photo . '</div>';
                }

                else {
                    echo 'no Thumbnail';
                }

                $content = wpautop($recent['post_content']);
                $trimmed_content = wp_trim_words( $content, 60, '...' );
                ;
                echo '<div class="trimmedContent">' . $content .'</div>';
                echo '<a class="button section1-2single-button" href="'. get_permalink($recent["ID"]) .'">weiter</a>';

                echo '</div>';

                echo '</div>';

                echo '</div>';



            }

            ?>

Upvotes: 1

Views: 5601

Answers (3)

ataylor
ataylor

Reputation: 101

The has_post_thumbnail() function takes an optional parameter $post which can be a post ID or a WP_Post object.

The default is the global $post object. You need to pass the post ID to tell WordPress to check the recent post in your loop instead of the global $post object. Like this:

if ( has_post_thumbnail( $recent['ID'] ) ) {
    $photo = get_the_post_thumbnail( $recent['ID'], 'large' );
    echo  '<div class="section1-2singlephoto">' . $photo . '</div>';
}

else {
    echo 'no Thumbnail';
}

Upvotes: 3

softbrewery
softbrewery

Reputation: 501

If you are outside of the loop, you need to provide a post ID:

if ( has_post_thumbnail( $post_id ) ) {
    // ...
}

Upvotes: 2

johnnyd23
johnnyd23

Reputation: 1705

You are passing an ID to the get_post_thumbnail() function in order to get a thumbanil for a specific post, so you should be checking for a thumbanil on that post as well by passing the ID to has_post_thumbnail()

if ( has_post_thumbnail( $recent['ID'] ) ) {

    $photo = get_the_post_thumbnail( $recent['ID'], 'large' );
    echo  '<div class="section1-2singlephoto">' . $photo . '</div>';

} else {

    echo 'no Thumbnail';

}

Upvotes: 1

Related Questions