Custom Fields - Display a thumbnail (and auto resize it)


/ Published in: PHP
Save to your folder(s)

Whilst there are a number of ways of displaying thumbnails, this is still my favourite (even if it does require the most work!). First, upload timthumb to /wp-content/themes/yourtheme/ and create an image 250px by 250px with your site’s logo and upload it to yourtheme/images/. This is the image that will be displayed if no custom field is displayed. Then, for every thumbnail, you’ll need to create it in Photoshop and upload it to your blog. Next, create a custom field ‘Image’ with the path of the image as the value of the custom field. Now for the code:


Copy this code and paste it in your HTML
  1. <?php $postimageurl = get_post_meta($post->ID, 'Image', true);
  2. if ($postimageurl) {
  3. ?>
  4. <img src="/wp-content/themes/NomeMag/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "Image", true); ?>
  5. &h=250&w=250&zc=1" alt="">
  6. <?php } else { ?>
  7. <img src="/wp-content/themes/NomeMag/images/wpshoutlogo.jpg" alt="WPShout.com | No image available" />
  8. (don't have a line break, just so it's readable)
  9. <?php } ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.