Posted By

piticstyle on 12/03/09


Tagged

image post wordpress images in posts


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

secondtwo
vintagekrug


Post Image in Wordpress Posts


 / Published in: PHP
 

URL: http://www.smashingmagazine.com/2009/05/13/10-custom-fields-hacks-for-WordPress/

  1. THE SOLUTION
  2.  
  3. 1. Start by creating a default image in Photoshop or Gimp. The size in my example is 200×200 pixels but is of course up to you. Name the image default.gif.
  4. 2. Upload your default.gif image to the image directory in your theme.
  5. 3. Open the index.php file and paste in the following code where you’d like the thumbnails to be displayed:
  6.  
  7. <?php $postimageurl = get_post_meta($post->ID, 'post-img', true);
  8. if ($postimageurl) {
  9. ?>
  10. <a href="<?php the_permalink(); ?>" rel="bookmark"><img src="<?php echo $postimageurl; ?>" alt="Post Pic" width="200" height="200" /></a>
  11. <?php } else { ?>
  12. <a href="<?php the_permalink(); ?>" rel="bookmark"><img src="<?php bloginfo('template_url'); ?>/images/wprecipes.gif" alt="Screenshot" width="200" height="200" /></a>
  13. <?php } ?>
  14.  
  15. 4. Save the file.
  16. 5. In each of your posts, create a custom field called post-img. Set its value as the URL of the image you’d like to display as a thumbnail.
  17.  
  18. Code explanation:
  19. The code looks for a custom field called post-img. If found, its value is used to display a custom thumbnail.
  20.  
  21. In case a post-img custom field is not found, the default image is used, so you’ll never have any posts without thumbnails.

Report this snippet  

You need to login to post a comment.