WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. rickmesser
    Member
    Posted 4 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>
    </div>
    <?php the_excerpt(); ?>
    </div>

    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?

    Thanks

  2. esmi
    Forum Moderator
    Posted 4 years ago #

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

    http://codex.wordpress.org/Template_Tags/has_post_thumbnail

  3. rickmesser
    Member
    Posted 4 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!

    Rick

  4. osceweb
    Member
    Posted 4 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