/ Published in: PHP
URL: http://www.smashingmagazine.com/2009/05/13/10-custom-fields-hacks-for-WordPress/
Expand |
Embed | Plain Text
THE SOLUTION 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. 2. Upload your default.gif image to the image directory in your theme. 3. Open the index.php file and paste in the following code where you’d like the thumbnails to be displayed: <?php $postimageurl = get_post_meta($post->ID, 'post-img', true); if ($postimageurl) { ?> <a href="<?php the_permalink(); ?>" rel="bookmark"><img src="<?php echo $postimageurl; ?>" alt="Post Pic" width="200" height="200" /></a> <?php } else { ?> <a href="<?php the_permalink(); ?>" rel="bookmark"><img src="<?php bloginfo('template_url'); ?>/images/wprecipes.gif" alt="Screenshot" width="200" height="200" /></a> <?php } ?> 4. Save the file. 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. Code explanation: The code looks for a custom field called post-img. If found, its value is used to display a custom thumbnail. In case a post-img custom field is not found, the default image is used, so you’ll never have any posts without thumbnails.
You need to login to post a comment.
