[resolved] How Do I Display a div, ONLY if the post has a post_thumbnail? (4 posts)

  1. rickmesser
    Posted 5 years ago #

    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?


  2. esmi
    Forum Moderator
    Posted 5 years ago #

    <?php if( has_post_thumbnail() ) :?>
    <div class="post_thumb">
    [ do stuff ]
    <?php endif;?>


  3. rickmesser
    Posted 5 years ago #

    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!


  4. osceweb
    Posted 5 years ago #

    I just have to say. Esmi..you 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.

Topic Closed

This topic has been closed to new replies.

About this Topic