Title: Adding Default Thumbnail
Last modified: August 20, 2016

---

# Adding Default Thumbnail

 *  [stiwdio](https://wordpress.org/support/users/stiwdio/)
 * (@stiwdio)
 * [14 years ago](https://wordpress.org/support/topic/adding-default-thumbnail/)
 * Wonder if someone to add to my code;
 * I have a simple latest post in the sidebar, but have the an image, if saved in
   the custom field, for the post as the background of a div with rounded corners;
 * It works well, but now I want to add a code to add a custom image if one isnt
   uploaded; Can someone help me out?
 * Cheers
 *     ```
       <?php
       $recentPosts = new WP_Query();
       $recentPosts->query('showposts=1');
       while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
       <?php $post_back = get_post_meta($post->ID, 'homepage-thumbnail', $single = true);?>
       <div class="newsimage" style="background: url('<?php echo $post_back; ?>') no-repeat; background-size: 100%;"></div>
       <span class="sidetitle"><a href="<?php the_permalink() ?>" rel="bookmark"
           title="Link to <?php the_title(); ?>">
       <?php the_title(); ?></a> </span><br />
       <?php echo get_excerpt(150); ?>
       <?php endwhile;?>
       </div>
       ```
   

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

 *  [Marco Milesi](https://wordpress.org/support/users/milmor/)
 * (@milmor)
 * [14 years ago](https://wordpress.org/support/topic/adding-default-thumbnail/#post-2803829)
 *     ```
       <?php if ( get_post_meta($post->ID, 'homepage-thumbnail', true) ) {?>
       <?php $post_back = get_post_meta($post->ID, 'homepage-thumbnail', $single = true);?>
       <?php endif; ?>
       ```
   
 * Then use <?php } else {?> to add code for your custom images.
 *  Thread Starter [stiwdio](https://wordpress.org/support/users/stiwdio/)
 * (@stiwdio)
 * [14 years ago](https://wordpress.org/support/topic/adding-default-thumbnail/#post-2803833)
 * Hi Milmor
 * Thanks for this, I haven’t got a clue though could you show me exactly hwo I 
   should write it?
 *  Thread Starter [stiwdio](https://wordpress.org/support/users/stiwdio/)
 * (@stiwdio)
 * [14 years ago](https://wordpress.org/support/topic/adding-default-thumbnail/#post-2803835)
 * Maybe I should add this is in the sidebar
 * Please can someone help me
 *  [Marco Milesi](https://wordpress.org/support/users/milmor/)
 * (@milmor)
 * [14 years ago](https://wordpress.org/support/topic/adding-default-thumbnail/#post-2803837)
 * Try with this:
 *     ```
       <?php
       if ( get_post_meta($post->ID, 'homepage-thumbnail', true) ) {
       $post_back = get_post_meta($post->ID, 'homepage-thumbnail', $single = true);
       echo '<div class="newsimage" style="background: url(' . $post_back . ') no-repeat; background-size: 100%;"></div>';
       } else {
       echo '<div class="newsimage" style="background: url(yourhttpimagepath) no-repeat; background-size: 100%;"></div>';
       }
       ?>
       ```
   
 * You have to replace “yourhttpimagepath” with the alternative image
 *  [prionkor](https://wordpress.org/support/users/prionkor/)
 * (@prionkor)
 * [14 years ago](https://wordpress.org/support/topic/adding-default-thumbnail/#post-2803839)
 * Further read on if else statement 🙂
 * [http://www.w3schools.com/php/php_if_else.asp](http://www.w3schools.com/php/php_if_else.asp)
   
   [http://php.net/manual/en/control-structures.elseif.php](http://php.net/manual/en/control-structures.elseif.php)
 *  Thread Starter [stiwdio](https://wordpress.org/support/users/stiwdio/)
 * (@stiwdio)
 * [14 years ago](https://wordpress.org/support/topic/adding-default-thumbnail/#post-2803841)
 * Thank you Milmor
    That worked well;

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

The topic ‘Adding Default Thumbnail’ is closed to new replies.

## Tags

 * [custom field](https://wordpress.org/support/topic-tag/custom-field/)
 * [default image](https://wordpress.org/support/topic-tag/default-image/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 3 participants
 * Last reply from: [stiwdio](https://wordpress.org/support/users/stiwdio/)
 * Last activity: [14 years ago](https://wordpress.org/support/topic/adding-default-thumbnail/#post-2803841)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
