Title: featured image PHP if loop &#8211; no empty div
Last modified: August 30, 2016

---

# featured image PHP if loop – no empty div

 *  Resolved [Jintro](https://wordpress.org/support/users/jintro/)
 * (@jintro)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/featured-image-php-if-loop-no-empty-div/)
 * I’ve been looking for code to show the feature image in a div so i can control
   the styling of that image.
    I am not good at PHP, but I try to fiddle with code
   snippets I find here and elsewhere. But I’ve been braeking my head on this one:
 * The code below works great. But I wonder how I can adjust it so that the div 
   doesn’t show when its empty. I don’t have this on a live site yet, sorry. But
   I also used some CSS to style the div, such as a border.
 * All help and suggestions are welcome.
 *     ```
       <?php
   
                   $thumb = get_post_thumbnail_id();
                   $img_url = wp_get_attachment_url( $thumb,'full' );
   
                   ?>
   
                   <div class="blogfeat">
                       <img class="blogfeatimg" src="<?php echo esc_url($img_url); ?>"/>
                   </div>
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [newbiesup](https://wordpress.org/support/users/newbiesup/)
 * (@newbiesup)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/featured-image-php-if-loop-no-empty-div/#post-6593441)
 * Just add a conditional statement.
 *     ```
       <?php
   
                   $thumb = get_post_thumbnail_id();
                   $img_url = wp_get_attachment_url( $thumb,'full' );
   
                   if ($img_url) {?>
   
                   <div class="blogfeat">
                       <img class="blogfeatimg" src="<?php echo esc_url($img_url); ?>"/>
                   </div>
       <?php }?>
       ```
   
 *  Thread Starter [Jintro](https://wordpress.org/support/users/jintro/)
 * (@jintro)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/featured-image-php-if-loop-no-empty-div/#post-6593553)
 * I had the markup of the if-statement wrong.
    This works perfect. Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘featured image PHP if loop – no empty div’ is closed to new replies.

## Tags

 * [div](https://wordpress.org/support/topic-tag/div/)
 * [feature image](https://wordpress.org/support/topic-tag/feature-image/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [Jintro](https://wordpress.org/support/users/jintro/)
 * Last activity: [10 years, 7 months ago](https://wordpress.org/support/topic/featured-image-php-if-loop-no-empty-div/#post-6593553)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
