<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Coder Plus &#187; Wordpress</title>
	<atom:link href="http://coderplus.com/category/wordpress/feed/" rel="self" type="application/rss+xml" />
	<link>http://coderplus.com/blog</link>
	<description></description>
	<lastBuildDate>Fri, 26 Feb 2010 06:21:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Beautiful Post Thumbnails for your Wordpress Blog</title>
		<link>http://coderplus.com/blog/2009/11/beautiful-post-thumbnails-for-your-wordpress-blog/</link>
		<comments>http://coderplus.com/blog/2009/11/beautiful-post-thumbnails-for-your-wordpress-blog/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 11:58:01 +0000</pubDate>
		<dc:creator>Coder</dc:creator>
				<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://coderplus.com/?p=242</guid>
		<description><![CDATA[In this tutorial, we will discuss the different methods to generate thumbnails and display them elegantly alongside your posts. This tutorial is meant to be used for the Wordpress Blogging Platform. We will do it in the timthumb, custom field and in the Wordpress Way]]></description>
			<content:encoded><![CDATA[<p>In this tutorial, we will discuss the different methods to generate thumbnails and display them elegantly alongside your posts. This tutorial is meant to be used for the Wordpress Blogging Platform. We will do it in the timthumb, custom field and in the Wordpress Way</p>
<h3>Usning the Timthumb thumbnail Generator</h3>
<p><strong>Timthumb</strong> is a thumbnail Generator script written in PHP and it requires the GD library. You can get the <strong>timthumb</strong> script from <a href="http://www.darrenhoyt.com/2008/04/02/timthumb-php-script-released/" target="_blank">Darren Hoyt</a> at <a href="http://timthumb.googlecode.com/svn/trunk/timthumb.php" target="_blank">Google Code</a> . First of all lets thank Darren for this magical script.</p>
<h3>How Stuff Works?</h3>
<p>For a neat look, we would be displaying post thumbnails along with post summarieson your wordpress home page and other archive pages. Goto the <a title="Post Thumbnails - Demo" href="http://coderplus.com/demos/postthumbnails/" target="_blank">DEMO PAGE</a> and see how it looks on Wordpress’s default Kubrick Theme.Now lets see how this works.We have two methods for selecting the image used for generating the post thumbnail.</p>
<p>1. Specifying the Image from which the thumbnail should be generated(We have to do it for each post by using a Wordpress Custom Field)</p>
<p>2. By automatically using the first image in the post for generating the post thumbnail(thumbnail will be generated automatically from the first image in the post)</p>
<h3>Our Plan</h3>
<p>Here we will use a combination of the above two methods. If the post has a Custom Image field value then we will use that specified image for generating the thumbnail .If it doesn&#8217;t have any image url specified in the custom field, then we will use the first image in the post, and if the post doesn&#8217;t have any images and it doesn&#8217;t have an image URL in the custom field, then we wont use the post thumbnail.This is a very simple Code. So everyone would be able to figure out how it works. First of all we have to get the timthumb script on your server.Create a folder named <strong>thumbnails</strong> in your <strong>wp-content</strong> folder . Upload the <strong>timthumb.php</strong> script into this folder. Make a subfolder named <strong>cache</strong> within the <strong>wp-content/thumbnails</strong> folder and change the permissions of this <strong>cache</strong> folder to <strong>777</strong>.</p>
<p>Before  we start lets get familiar with some Wordpress Template tags.</p>
<h3>Template Tags</h3>
<p>1.<strong>&lt;?php the_content();  ?&gt;</strong> – This template tag outputs the entire wordpress post.</p>
<p>2.<strong>&lt;?php the_excerpt(); ?&gt;</strong> – This template tag outputs the post summary.If you haven&#8217;t manually added  a post excerpt,then wordpress will automatically generate one for you.So this template tag can be used to display the post summary.</p>
<h3>Ok lets do it.</h3>
<p>We will start with the Wordpress home page. So edit your theme’s index.php file. You would probably see something similar to</p>
<div class="code">
<pre>&lt;?php the_content();  ?&gt;</pre>
</div>
<p>. We will have to replace this with</p>
<div class="code">
<pre>&lt;?php the_excerpt(); ?&gt; .</pre>
</div>
<p>Now we are half way through. Your wordpress home page will now be displaying post summaries instead of showing the entire post.Now we have to display the thumbnails alongside the post summary.For this you will have to add the thumbnail generation(+display) code just above</p>
<div class="code">
<pre>&lt;?php the_excerpt(); ?&gt;</pre>
</div>
<h3>Code For Thumbnail Generation + Display</h3>
<div class="code">
<pre>  &lt;?php  $custom_values = get_post_custom_values("<strong>thumb_source</strong>");
   if (isset($custom_values[0]))  $image_source= $custom_values[0];
  else
  {$id =$post-&gt;ID;
  $the_content =$wpdb-&gt;get_var("SELECT post_content FROM   $wpdb-&gt;posts WHERE ID = $id");
  $pattern = '!&lt;img.*?src="(.*?)"!';
  preg_match_all($pattern, $the_content, $matches);
  $image_source = $matches['1'][0];
  }if (isset($image_source))
  {?&gt;
  &lt;a href="&lt;?php the_permalink();?&gt;"&gt;&lt;img class="cute-post-thumbnail" src="<strong>http://www.yourblog.com</strong>/wp-content/thumbnails/timthumb.php?src=&lt;?php echo $image_source;?&gt;&amp;w=<strong>180</strong>&amp;h=<strong>180</strong>&amp;zc=<strong>1</strong>&amp;q=<strong>85</strong>"  alt="&lt;?php the_title();?&gt;"&gt;&lt;/a&gt;
  &lt;?php }?&gt;</pre>
</div>
<h3>Ok what does this Code do?</h3>
<p>It tries to fetch the custom image field ,  <strong>thumb_source</strong> (you can use any custom field name.In this tutorial we will use <strong>thumb_source </strong>as our custom field name) . If you have added a custom field <strong>thumb_source</strong> with the image URL, then the code will use that particular  image for thumbnail generation.If it is unable to find the custom field, then it will scan the post to see if it has any images.If it has got any images, then it will use the first image in the post for generating the thumbnail.If the post doesn’t have any image and it doesn’t have a custom field image, then the script wont do anything.In the code you should replace <strong>http://www.yourblog.com</strong> with your wordpress blog’s URL. You can also alter the highlighted values in the code.</p>
<blockquote><p><strong>w=180</strong> the width of the generated thumbnail</p>
<p><strong>h=180</strong> the height of the thumbnail</p>
<p><strong>zc=1</strong> this will crop the image.if you set it to 0, it will resize instead.</p>
<p><strong>q=85</strong> this determines the quality of the generated thumbnail. maximum value is 100</p></blockquote>
<p>Now you should see the thumbnail image and the post summary on the Wordpress home page. The thumbnail image is not styled properly.This is our next task.</p>
<h3>Styling the The Thumbnail Image</h3>
<p>Now we have to edit our theme’s stylesheet file(style.css) and add the following styles there</p>
<div class="code">
<pre>.cute-post-thumbnail{float:left;width:190px;height:190px;padding:10px;margin-right:10px;margin-bottom:10px;background:#d5d6d7;border: 3px solid #ccc;}</pre>
</div>
<p>You can surely alter the styles and make it even more beautiful and appealing .</p>
<p>Now we are finished with the wordpress home page.Now repeat the same process for the wordpress archive pages(tag,category search and other archives) by editing archives.php,search.php etc  in a similar manner.</p>
<h3>Extra Notes</h3>
<p>1.<strong>Timthumb</strong> can generate thumbnails of internal images only.So if you are using images hosted on some other sites,then <strong>timthumb</strong> wont be able to generate a thumbnail for you.</p>
<p>2.If you don’t want to use <strong>timthumb</strong>, you can make thumbnails by your own and then upload it somewhere and add the image URL as custom field.In this case you will have to modify the thumbnail code to</p>
<div class="code">
<pre>&lt;?php  $custom_values = get_post_custom_values("thumb_source");
if (isset($custom_values[0]))  {$image_source= $custom_values[0];?&gt;
&lt;a href="&lt;?php the_permalink();?&gt;"&gt;&lt;img class="cute-post-thumbnail" src="&lt;?php echo $image_source;?&gt;"  alt="&lt;?php the_title();?&gt;"&gt;&lt;/a&gt;
&lt;?php }?&gt;</pre>
</div>
<h3>Doing it in the Wordpress Way</h3>
<p>As krembo99 has suggested in the comment,we can make use of the default wordpress functions to grab the thumbnails.To do this First of all goto your Wodpress Admin Panel &gt; Settings &gt; Media and set the thumbnail size to the size that you want.</p>
<p>Now use this Thumbnail generation Code instead</p>
<pre>&lt;?php
  $attachments = get_children( array('post_parent'  =&gt; get_the_ID(),
                'post_type'      =&gt; 'attachment',
                'numberposts'    =&gt; 1, // show all -1
                'post_status'    =&gt; 'inherit',
                'post_mime_type' =&gt; 'image',
                'order'          =&gt; 'ASC',
                'orderby'        =&gt; 'menu_order ASC'
                ) );

if($attachments)
{foreach ( $attachments as $attachment_id =&gt; $attachment ) {?&gt;
    &lt;a href="&lt;?php the_permalink();?&gt;"&gt;&lt;?php echo wp_get_attachment_image( $attachment_id,'thumbnail' );?&gt;&lt;/a&gt;;
&lt;?php }}?&gt;</pre>
<p>Now here the image is assigned a class attachment-thumbnail . So we will have to alter the styles to</p>
<pre>.attachment-thumbnail{float:left;width:190px;height:190px;padding:10px;margin-right:10px;margin-bottom:10px;background:#d5d6d7;border: 3px solid #ccc;}</pre>
<p>This method will use the thumbnail version of the first image in your post.</p>
<h3>Doing in on the New Beta Version Of Wordpress using Wordpress Template Tags</h3>
<p>Wordprerss 2.9, which is now in beta comes with some additional template tags.So the code becomes lighter</p>
<pre>&lt;?php if ( has_post_image() ) : ?&gt;
    &lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_post_image('thumbnail'); ?&gt;&lt;/a&gt;
&lt;?php endif; ?&gt;</pre>
<p>If you require help with the following code, post a comment and I will respond to your inquiry .</p>
]]></content:encoded>
			<wfw:commentRss>http://coderplus.com/blog/2009/11/beautiful-post-thumbnails-for-your-wordpress-blog/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
	</channel>
</rss>
