Support » Fixing WordPress » How Do I Display a div, ONLY if the post has a post_thumbnail?

  • Resolved rickmesser


    Using the new “the_post_thumbnail” function. Within The Loop, I want to display post thumbnails for all my posts. Here’s what I’m doing:

    <div class="entry">
    <div class="post_thumb">
    <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
    <?php the_excerpt(); ?>

    Works, great! Love it.

    But, I ONLY want the div containing the thumbnail (<div class=”post_thumb”>) to show up if there’s a thumbnail. Otherwise, there’s just an empty div showing up on posts that have not been assigned a post thumbnail. I’m not an expert at php but I’m learning, and I know there’s some sort of “conditional” statement I can create to achieve this, Like, “if function exists the_post_thumbnail…”?

    Any php experts out there to throw a quick hand?


Viewing 3 replies - 1 through 3 (of 3 total)
  • <?php if( has_post_thumbnail() ) :?>
    <div class="post_thumb">
    [ do stuff ]
    <?php endif;?>

    Awesome esmi! You’ve done it again!

    Thank-you so much! Are you just one person!?! You always answer my posts! And I see you all over whenever I search a question I have!

    Thanks again!


    I just have to say. are a legend!

    I have been sat here trying for the past hour and a half and you just solved my problem! Everytime i put in the code it just didnt work! Then I saw your post and realised I had missed out the

    <?php endif;?>!!

    Silly mistake!

    Thanks. Much appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How Do I Display a div, ONLY if the post has a post_thumbnail?’ is closed to new replies.