<?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>Justin Tadlock &#187; Custom Fields</title>
	<atom:link href="http://justintadlock.com/tags/custom-fields/feed" rel="self" type="application/rss+xml" />
	<link>http://justintadlock.com</link>
	<description>Life, Blogging, and WordPress</description>
	<lastBuildDate>Fri, 11 May 2012 00:27:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Custom Fields For Feeds: WordPress Plugin</title>
		<link>http://justintadlock.com/archives/2008/01/27/custom-fields-for-feeds-wordpress-plugin</link>
		<comments>http://justintadlock.com/archives/2008/01/27/custom-fields-for-feeds-wordpress-plugin#comments</comments>
		<pubDate>Sun, 27 Jan 2008 20:07:38 +0000</pubDate>
		<dc:creator>Justin Tadlock</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Custom Fields]]></category>
		<category><![CDATA[WordPress Plugins]]></category>

		<guid isPermaLink="false">http://justintadlock.com/archives/2008/01/27/custom-fields-for-feeds-wordpress-plugin</guid>
		<description><![CDATA[Description: This puts images or videos into your feeds through the use of custom fields. You can alter the custom field Keys and what is displayed. Version 1.0.1 Beta License: GPL Download Plugin () About this plugin One of the things I wanted to accomplish in the custom fields tutorial series is adding the custom [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://justintadlock.com/wp-content/uploads/2008/01/custom-feed.gif" title="WordPress custom fields for feeds" alt="WordPress custom fields for feeds" class="center" /></p>
<ul>
<li><strong>Description:</strong> This puts images or videos into your feeds through the use of custom fields.  You can alter the custom field Keys and what is displayed.</li>
<li><strong>Version 1.0.1 Beta</strong></li>
<li><strong>License:</strong> GPL</li>
<li><a href="http://justintadlock.com/downloads/custom-fields-for-feeds.zip" title="Download the Custom Fields for Feeds WordPress plugin">Download Plugin (8023)</a></li>
</ul>
<h3>About this plugin</h3>
<p>One of the things I wanted to accomplish in the <a href="http://justintadlock.com/archives/2007/10/24/using-wordpress-custom-fields-introduction" title="Using custom fields tutorial series"> custom fields tutorial series</a> is adding the custom fields to the feed.  So, if you&#8217;re reading this post through your feed reader, I hope there&#8217;s an image at the top of this post.  If not, I suppose something went wrong.  If you&#8217;re not reading this in a feed reader, then I suggest you <a href="http://feeds.feedburner.com/justintadlock" title="Subscribe to the feed"> subscribe to the feed</a> now.</p>
<p>After completing work on the <a href="http://justintadlock.com/archives/2007/10/24/using-wordpress-custom-fields-introduction" title="Stucture WordPress theme"> Structure theme</a>, I decided that I needed to figure this thing out at some point.  The theme uses multiple custom fields to display content on the site.  In part, this plugin was made for that theme.</p>
<p>I only came up with this idea after I ran across <a href="http://ma.tt" title="Matt Mullenweg's"> Matt Mullenweg&#8217;s</a> plugin, <a href="http://wordpress.org/extend/plugins/adsense-for-feeds" title="Adsense for Feeds"> Adsense for Feeds</a>, the other day.  Basically, I took that script and made it display custom fields instead of ads.</p>
<p>Right now, it&#8217;s still in beta, and I&#8217;m sure I&#8217;ll work on it more.  I just wanted to put this out there for those using the theme.  Other than those using my themes, I&#8217;m sure this might come in handy if you like tinkering with PHP or use a lot of custom fields on your site.</p>
<p>The plugin was created to be modified.  There&#8217;s no clear way to make this a &#8220;one-size-fits-all&#8221; plugin because I have no way of knowing what or how you&#8217;d want to display your content in your feed.</p>
<h3>Using the plugin</h3>
<p>You need to upload the &#8220;custom-fields-for-feeds&#8221; folder to your WordPress plugins folder.  Then activate it from your WordPress administration panel.</p>
<p>To use this plugin out of the box, you must do a few things.  First, you must know how to <a href="http://justintadlock.com/archives/2007/10/24/using-wordpress-custom-fields-introduction" title="Using custom fields tutorial series"> use custom fields</a>.  This is imperative.</p>
<p>The plugin will display one of four different items.  Each one of these are <strong> Keys</strong>.  Note that custom field <strong> Keys</strong> are case-sensitive.  This is the order of the list the plugin checks for the custom fields.  If one is used, then the others aren&#8217;t displayed.</p>
<ul>
<li>Video</li>
<li>Image</li>
<li>Feature Image</li>
<li>Thumbnail</li>
</ul>
<p>To use the &#8220;Video&#8221; custom field and if you&#8217;re not using one of my themes, you need to look at this tutorial on <a href="http://justintadlock.com/archives/2008/02/24/options-wordpress-theme/attachment/787" title="How to add videos to your WordPress sidebar"> adding videos</a>.  It explains a lot.</p>
<p>If you&#8217;re using &#8220;Image,&#8221; &#8220;Feature Image,&#8221; or &#8220;Thumbnail,&#8221; it wouldn&#8217;t hurt to follow this tutorial on <a href="/attachment/682" title="How to add image to posts with WordPress custom fields"> adding images to posts</a>.  I go into detail on how to accomplish this.</p>
<p><strong>I&#8217;ll assume from this point that you know how to use WordPress custom fields.</strong></p>
<p>To add a video (YouTube, Google, MetaCafe, etc.) to your feed using custom fields, you need to create a <strong> Key</strong> named &#8220;Video.&#8221;  Give it a <strong> Value</strong> of the video&#8217;s &#8220;embed URL.&#8221;</p>
<p>To add an image, give it a <strong> Key</strong> of &#8220;Image,&#8221; &#8220;Feature Image,&#8221; or &#8220;Thumbnail.&#8221;  The <strong> Value</strong> should be the &#8220;URL of the image&#8221; you want to use.</p>
<p>There&#8217;s also the option of adding alt text to your images.  The <strong> Keys</strong> are:</p>
<ul>
<li>Image Alt</li>
<li>Feature Image Alt</li>
<li>Thumbnail Alt</li>
</ul>
<p>Just give them a <strong> Value</strong> of the alt text you want for the image.</p>
<p>The plugin will display the video or image at the beginning of the post in your feed.  There&#8217;s also a smaller sample file that you can play around with.</p>
<h3>Final thoughts</h3>
<p>I&#8217;d like to push this plugin a little farther, give users the ability to display custom fields in various different ways.  Right now, I want to allow users of my Structure theme to have their custom fields displayed in their feeds.</p>
<p>Please do not ask me questions about how to use custom fields on this post.  This plugin is for those that already know how to use custom fields.  If you want to learn how to use them, read these articles:</p>
<ul>
<li><a href="http://codex.wordpress.org/Using_Custom_Fields" title="Using Custom Fields">Using custom fields guide</a> from the WordPress Codex.</li>
<li><a href="http://justintadlock.com/archives/2007/10/24/using-wordpress-custom-fields-introduction" title="Using WordPress custom fields introduction">Tutorial series</a> on using custom fields.</li>
</ul>
<p>Feel free to push this plugin to its limits.  This is really just a starting point.  I&#8217;d love to hear your ideas on how to develop this plugin into something more powerful.</p>
<p>Once I click the &#8220;Publish&#8221; button, we&#8217;ll see if this thing is working.  I hope my image shows up in your feed reader.</p>
<p><strong>Update:</strong> The plugin definitely works.</p>
]]></content:encoded>
			<wfw:commentRss>http://justintadlock.com/archives/2008/01/27/custom-fields-for-feeds-wordpress-plugin/feed</wfw:commentRss>
		<slash:comments>92</slash:comments>
		</item>
		<item>
		<title>How to add videos to your WordPress sidebar</title>
		<link>http://justintadlock.com/archives/2008/01/25/how-to-add-videos-to-your-wordpress-sidebar</link>
		<comments>http://justintadlock.com/archives/2008/01/25/how-to-add-videos-to-your-wordpress-sidebar#comments</comments>
		<pubDate>Fri, 25 Jan 2008 14:50:53 +0000</pubDate>
		<dc:creator>Justin Tadlock</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Custom Fields]]></category>
		<category><![CDATA[WordPress Tutorials]]></category>

		<guid isPermaLink="false">http://justintadlock.com/archives/2008/01/25/how-to-add-videos-to-your-wordpress-sidebar</guid>
		<description><![CDATA[YouTube has become a bit of a phenomenon on the Internet, and everyone's rushing to embed videos into their blogs.  This is actually a fairly neat thing, especially if you blog a lot about videos or have videos that will emphasize the importance of your posts.]]></description>
			<content:encoded><![CDATA[<p>YouTube has become a bit of a phenomenon on the Internet, and everyone&#8217;s rushing to embed videos into their blogs.  This is actually a fairly neat thing, especially if you blog a lot about videos or have videos that will emphasize the importance of your posts.</p>
<p>In my last two themes, <a href="" title="Structure WordPress theme: A CMS theme for big and small themes"> Structure</a> and <a href="http://justintadlock.com/archives/2007/12/30/the-year-my-life-changed" title="Visionary: A WordPress theme for news and magazine sites"> Visionary</a>, I created a block that displays the latest video from a category titled &#8220;Video&#8221; on the front page of the site.  Take a look at a <a href="http://demo.justintadlock.com/index.php?wptheme=Structure" title="Structure WordPress theme demo"> live demo</a> that uses the code in this tutorial.</p>
<p>The technique I&#8217;ll show you here is completely valid XHTML and works with every browser I could think to test it on.  In the example, we&#8217;ll use a YouTube video, but there are a number of video sites that you can use this technique on.  This tutorial assumes that you are somewhat familiar with XHTML, PHP, CSS, YouTube, and WordPress.  You must also be writing a post because we&#8217;ll need to use its custom field functionality.</p>
<p>For the sake of this tutorial, we&#8217;ll put our video in the sidebar of the theme.  You can, of course, put the video wherever you want later.</p>
<h3>Find your video</h3>
<p>This is obviously the most important step in the process.  The example I&#8217;ll use is from YouTube.  You can check out the <a href="http://youtube.com/watch?v=FE_XpRmtcJw" title="Sweatin: YouTube Video Example"> Sweatin&#8217;</a> video and use it as an example.</p>
<p>Once you&#8217;ve found your video, you need to get the embed URL.  On the video&#8217;s page, you see a section titled &#8220;Embed.&#8221;  You need to copy that code (see the highlighted portion of this image from our YouTube video):</p>
<p><img src="http://justintadlock.com/blog/wp-content/uploads/2008/01/youtube-highlight.gif" alt="Highlight the YouTube video embed code" class="center i425x100" /></p>
<p>The embed code that you copied should look like this (yes, it&#8217;s a mess):</p>
<pre><code>&lt;object width="425" height="355"&gt;&lt;param name="movie" value="http://www.youtube.com/v/FE_XpRmtcJw&#038;rel=1"&gt;&lt;/param&gt;&lt;param name="wmode" value="transparent"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/FE_XpRmtcJw&#038;rel=1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"&gt;&lt;/embed&gt;&lt;/object&gt;</code></pre>
<p>The only part of that code we need is the URL you see in there (notice the missing &#8220;<code>&amp;rel</code>&#8221; at the end):</p>
<pre><code>http://www.youtube.com/v/FE_XpRmtcJw</code></pre>
<p>Now, hold on to that for later.</p>
<h3>Write your post</h3>
<p>The next thing you want to do is write a post about your video.  That&#8217;s the entire reason you&#8217;re doing this, right?  When you&#8217;ve finished your post, you need to create a custom field.  This part is fairly simple.</p>
<p>At the bottom of the &#8220;Write Post&#8221; screen there is a section labeled &#8220;Custom Fields.&#8221;  There are two things you can input, <strong> Key</strong> and <strong> Value</strong>.  The drop-down list is for custom fields you&#8217;ve already used.  In the <strong> Key</strong> input box, type &#8220;Video&#8221; (make sure you capitalize &#8220;Video&#8221; because custom fields are case-sensitive).</p>
<p>In the <strong> Value</strong> input box, type &#8220;<code>http://www.youtube.com/v/FE_XpRmtcJw</code>.&#8221;  Yes, that&#8217;s the URL of the video you want to use.</p>
<p><img src="http://justintadlock.com/blog/wp-content/uploads/2008/01/video-custom-field.gif" alt="Inputting a custom field for adding YouTube videos to your site" class="center i425x100" /></p>
<p>Click the button &#8220;Add Custom Field&#8221; and you&#8217;re done, at least with the posting part.  Of course, we still have to code this thing.</p>
<h3>The code</h3>
<p>As I said earlier, we will put our code in the sidebar, but you can place it in many different places.  First, open &#8220;sidebar.php&#8221; in your favorite text editor.  The code we&#8217;ll input will probably have to be integrated into whatever type of menu system you have in your sidebar.</p>
<p>Now, you&#8217;ll lay down the framework.</p>
<pre><code>&lt;div id="sidebar"&gt;
	&lt;div class="video"&gt;
	&lt;div&gt;
		&lt;!-- All other code will go here --&gt;
	&lt;/div&gt;
	&lt;/div&gt;&lt;!-- video --&gt;
&lt;/div&gt;&lt;!-- sidebar --></code></pre>
<p>Next, we need to query the posts and begin the loop.  In our query, we call for the latest video in the &#8220;video&#8221; category.  You could easily change this to query videos by tag by changing &#8220;<code>category_name=video</code>&#8221; to &#8220;<code>tag=video</code>.&#8221;</p>
<pre><code>&lt;?php
// This file displays the latest video article on the front page
	rewind_posts();

// Query one post from "video" category
	$my_query = new WP_Query('category_name=video&#038;showposts=1');
	while ($my_query-&gt;have_posts()) : $my_query-&gt;the_post();</code></pre>
<p>We will grab the custom field information and display the post title after that.</p>
<pre><code>// Get the "Video" custom field Key as an array (displays single Value in the video block)
	$video = get_post_custom_values($key = 'Video');
?&gt;

	&lt;h3&gt;&lt;a href="&lt;?php the_permalink(); ?&gt;" title="&lt;?php the_title(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h3&gt;</code></pre>
<p>Now, for the bread and butter of this application &mdash; a valid XHTML video embed.  We check to see if there&#8217;s a custom field <strong> Key</strong> named &#8220;Video&#8221; with a <strong> Value</strong>.  If there is, the video is displayed.  You can also change the width and height of the video player in this part.</p>
<pre><code>&lt;div class="v&lt;?php echo $i; ?&gt; video"&gt;
&lt;?php
// Check to see if custom field "Video" is set and if it has anything in the "Value" field.
	if(isset($video[0]) &#038;&#038; strcmp($video[0],'')!= 0)  {

// Display valid XHTML player for YouTube, Google, MetaCafe, and other video sites
// "echo $video[0];" displays the first item in the array for the custom field "Video"
?&gt;

&lt;object type="application/x-shockwave-flash" data="&lt;?php echo $video[0]; ?&gt;" style="width: 290px; height: 242px; border: none; padding: 0; margin: 0;" id="video-block-&lt;?php echo $i; ?&gt;"&gt;
	&lt;param name="movie" value="&lt;?php echo $video[0]; ?&gt;" /&gt;
	&lt;param name='wmode' value='transparent' /&gt;
	&lt;param name="quality" value="best" /&gt;
	&lt;param name="bgcolor" value="transparent" /&gt;
	&lt;param name="FlashVars" value="playerMode=embedded" /&gt;
&lt;/object&gt;

&lt;?php
	} // endif</code></pre>
<p>All that&#8217;s left to do now is to add a little error checking code and close off the loop.  This will make sure you&#8217;ve added some type of <strong> Value</strong> to the custom field <strong> Key</strong> of &#8220;Video.&#8221;</p>
<pre><code>// If there is no Value for the custom field Key "Video"
else {

// echo error checking to check custom field errors
	echo 'Did not add a video URL to the custom field &lt;strong&gt; Key&lt;/strong&gt; of "Video"';
	echo '&lt;!-- This user did not add the video URL to the correct custom field --&gt;';

	} // endelse
?&gt;
&lt;/div&gt; &lt;!-- video --&gt;
&lt;?php endwhile; ?&gt;</code></pre>
<p>Save &#8220;sidebar.php.&#8221;  That&#8217;s it for the most part.  You might want to consider a few style rules to make it look a little prettier.</p>
<h3>Stylesheet</h3>
<p>Open &#8220;style.css&#8221; to add some CSS rules for your extra video feature.  Here&#8217;s a starting point, but you&#8217;ll have to edit it to fit your needs.  This code is meant to work with the code above.  So, if you&#8217;ve changed the width and height of your player, you might have to accomodate.</p>
<pre><code>.video {
	display: block;
	float: left;
	overflow: hidden;
	width: 288px;
	margin: 0 0 10px 0;
	padding: 2px 5px 10px 5px;
	text-align: center;
	}
.video div {
	overflow: hidden;
	margin: 0 auto;
	padding: 0;
	text-align: center;
	display: block;
	}
object { padding: 0; margin: 0; }</code></pre>
<h3>The finished product</h3>
<p>I hope by now that you haven&#8217;t had too many headaches getting this to work.  Here&#8217;s an example of how this should look in a completed sidebar.</p>
<p><img src="http://justintadlock.com/blog/wp-content/uploads/2008/01/video-finished.jpg" alt="Finished version of the adding YouTube videos to your WordPress sidebar tutorial" class="center i300x225" /></p>
<p>We&#8217;re completely finished now.  I hope you&#8217;ve enjoyed this tutorial and add a video section to your site today.  Be sure to <a href="http://feeds.feedburner.com/justintadlock" title="Subscribe to the feed"> subscribe to the feed</a> to get more WordPress tutorials.</p>
]]></content:encoded>
			<wfw:commentRss>http://justintadlock.com/archives/2008/01/25/how-to-add-videos-to-your-wordpress-sidebar/feed</wfw:commentRss>
		<slash:comments>57</slash:comments>
		</item>
		<item>
		<title>WordPress Custom Fields: Book Reviews Pt. 2</title>
		<link>http://justintadlock.com/archives/2007/11/20/wordpress-custom-fields-book-reviews-pt-2</link>
		<comments>http://justintadlock.com/archives/2007/11/20/wordpress-custom-fields-book-reviews-pt-2#comments</comments>
		<pubDate>Tue, 20 Nov 2007 21:59:36 +0000</pubDate>
		<dc:creator>Justin Tadlock</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Custom Fields]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://justintadlock.com/archives/2007/11/20/wordpress-custom-fields-book-reviews-pt-2</guid>
		<description><![CDATA[This part of the tutorial will cover making things look a little prettier. It is a more advanced PHP tutorial than any of the previous tutorials in the overall custom fields series. Basically, we will do a lot of work, but it won&#8217;t look like we&#8217;ve accomplished much. We still have to get much of [...]]]></description>
			<content:encoded><![CDATA[<p>This part of the <a href="/attachment/716" title="Using WordPress custom fields to create a reviews site"> tutorial</a> will cover making things look a little prettier.  It is a more advanced PHP tutorial than any of the previous tutorials in the overall custom fields series.  Basically, we will do a lot of work, but it won&#8217;t look like we&#8217;ve accomplished much.  We still have to get much of the basic code out of the way first.</p>
<p>By the end of this part of the tutorial, you may be wondering why in the hell you would go through all this trouble to display a linked image correctly and a few other things when you could&#8217;ve just as quickly typed all this in the post box.  And, you are right to think this.  Here are some reasons why this way can be useful:</p>
<ul>
<li>You can change the look of all your reviews and their custom fields without having to edit each post.</li>
<li>You can pull these custom fields out to use them in other areas of the site, like a reviews page.</li>
<li>You can display these things with your excerpts in your archives, categories, and search results.</li>
<li>There really is no limit to what you can do with WordPress custom fields.</li>
</ul>
<p>Although some of the &#8220;cooler&#8221; things are yet to come, we have to get the basics out of the way first.</p>
<h3>Adding a rating system:</h3>
<p>Now, that you&#8217;ve played around with the first part of the tutorial and you&#8217;re ready to move on, there is one thing I would like you to add to the original part of the code.  <a href="http://donalyza.com" title="Donalyza"> Donalyza</a> asked how she could add a rating system to her music reviews.</p>
<p>To add a rating system, the first thing you should do is create a <strong> Key</strong> named <strong> Rating</strong>.  Then, give it a <strong> Value</strong> of <strong> your rating</strong>.  For this tutorial, I&#8217;ll use a 1 &#8211; 5 rating system, but you can easily change this to 1 &#8211; 10, ABC, etc.</p>
<p>Open your file from the previous tutorial (home.php or single.php) and find the first code we entered:</p>
<pre><code>&lt;?php
// The book image
$image = get_post_meta($post-&gt;ID, 'Image', $single = true);

// The image alt text
$image_alt = get_post_meta($post-&gt;ID, 'Image Alt', $single = true);

// Image class (used for alignment, sizing, or styling using CSS)
$image_class = get_post_meta($post-&gt;ID, 'Image Class', $single = true);

// Book Author
$author = get_post_meta($post-&gt;ID, 'Author', $single = true);

// Outside link for book
$book_link = get_post_meta($post-&gt;ID, 'Book Link', $single = true);

// Title of the book
$book_title = get_post_meta($post-&gt;ID, 'Book Title', $single = true);

// Recommend? Yes or No
$recommend = get_post_meta($post-&gt;ID, 'Recommend', $single = true);

// Published date of book
$published_date = get_post_meta($post-&gt;ID, 'Published Date', $single = true);
?&gt;</code></pre>
<p>Add this after the &#8220;published&#8221; part, but before the <code> ?&gt;</code>:</p>
<pre><code>// Book Rating
$rating = get_post_meta($post->ID, 'Rating', $single = true);</code></pre>
<p>At the end of this post, I&#8217;ll show you how to display your ratings.</p>
<h3>The more complicated stuff:</h3>
<p>I&#8217;ll walk you through displaying your image, creating a link around that image, and a few other things in this part of the tutorial.  These are techniques that we&#8217;ll use later on.  So, it&#8217;s best to learn them now.</p>
<p>Find these lines from the previous part of the tutorial and delete them.  We&#8217;ll use these things, but in a more creative way.</p>
<pre><code>// If there's an image
if($image !== '') {
	echo '&lt;p&gt;&lt;strong&gt;Image URL:&lt;/strong&gt; ' . $image . '&lt;/p&gt;';
	}

// If there's image alt text
if($image_alt !== '') {
	echo '&lt;p&gt;&lt;strong&gt;Image Alt:&lt;/strong&gt; ' . $image_alt . '&lt;/p&gt;';
	}

// If there's an image CSS class
if($image_class !== '') {
	echo '&lt;p&gt;&lt;strong&gt;Image Class:&lt;/strong&gt; ' . $image_class . '&lt;/p&gt;';
	}

// If there's a book link
if($book_link !== '') {
	echo '&lt;p&gt;&lt;strong&gt;Book Link:&lt;/strong&gt; ' . $book_link . '&lt;/p&gt;';
	}</code></pre>
<p>Everything from this point forward relies heavily on you having an image to display with your review.  This is vital to this tutorial and the code you are about to see.  What we&#8217;re doing is checking to see if there&#8217;s an image and displaying it.  We will add alt text, a link, link title text, and an image class.  We&#8217;re interweaving all these elements together.  I ran through displaying the text on screen in the previous tutorial, so you could get a feel for how things work.</p>
<p>Replace the code you deleted with this (this should be above the line that reads <strong> // If there&#8217;s a book title</strong>):</p>
<pre><code>// If there's an image
if($image !== '') {
	echo '&lt;p&gt;';
	// If there's a book link
	if($book_link !== '') {
		echo '&lt;a href="' . $book_link . '" title="';
			// If there's image alt text
			if($image_alt !== '') echo $image_alt;
			else echo the_title();
		echo '"&gt;';
		} // end if

	// Show the image
	echo '&lt;img src="' . $image . '"';
		// If there's an image class
		if($image_class !== '') echo 'class="' . $image_class . '"';
	echo 'alt="';
		// If there's image alt text
		if($image_alt !== '') echo $image_alt;
		else echo the_title();
	echo '" /&gt;';

	// If there's a book link, close it off
	if($book_link !== '') {
		echo '&lt;/a&gt;';
		} //end if
	echo '&lt;/p&gt;';
	}</code></pre>
<p>Yes, the code just got crazy all of a sudden.  I urge you to take the time to read through this thoroughly to understand how the PHP works.  It&#8217;s not really complicated, but it can be overwhelming for newcomers to look at.  Feel free to ask questions on anything that&#8217;s going on in that part of the code.  I tried to leave plenty of comments to help you along.</p>
<h3>Customizing the recommendation area:</h3>
<p>In this part of the custom fields tutorial, we&#8217;ll add an image to the &#8220;Recommend&#8221; section.  You might also notice that the code is a little different from the previous tutorial, but it has now been updated.  This was a slight mistake I made, and you should make sure you update it.</p>
<p>Scroll down to the recommendation part of your code, which looks like this:</p>
<pre><code>// If there's a recommendation
if($recommend == 'yes' || $recommend == 'Yes') {
	echo '&lt;p&gt;&lt;strong&gt;Recommend?&lt;/strong&gt; ' . $recommend . '&lt;/p&gt;';
	}
elseif($recommend == 'no' || $recommend == 'No') {
	echo '&lt;p&gt;&lt;strong&gt;Recommend?&lt;/strong&gt; ' . $recommend . '&lt;/p&gt;';
	}
else { echo ''; }
?&gt;</code></pre>
<p>Replace it with this code:</p>
<pre><code>// If there's a recommendation
if($recommend == 'yes' || $recommend == 'Yes') {
	echo '&lt;p&gt;&lt;strong&gt;Recommend?&lt;/strong&gt; &lt;img src="/path/to/your/image/recommend-yes.gif" alt="I recommend this book!" /&gt;&lt;/p&gt;';
	}
elseif($recommend == 'no' || $recommend == 'No') {
	echo '&lt;p&gt;&lt;strong&gt;Recommend?&lt;/strong&gt; &lt;img src="/path/to/your/image/recommend-no.gif" alt="Only touch this book if your life depends on it!" /&gt;&lt;/p&gt;';
	}
else { echo ''; }</code></pre>
<p>You&#8217;ll need to upload an image for your &#8220;Yes&#8221; and &#8220;No&#8221; recommendations.  You&#8217;ll also need to change the path to the image in the above code.</p>
<h3>Displaying your ratings:</h3>
<p>Now that you&#8217;ve given your book, album, movie, game, or whatever a rating, you&#8217;ll want to display it.  Instead of the common if/else statement, we&#8217;ll use a PHP switch statement to execute this (this works even better if you have a 1 &#8211; 10 rating system.</p>
<p>Below the last code we added for the recommendations, add this code:</p>
<pre><code>// If there's a rating
if($rating !== '') {
	echo '&lt;p&gt;&lt;strong&gt;Rating: &lt;/strong&gt;';
	switch ($rating) {
	// If the rating is 0
	case 0:
		echo $rating;
		break;
	// If the rating is 1
	case 1:
		echo $rating;
		break;
	// If the rating is 2
	case 2:
		echo $rating;
		break;
	// If the rating is 3
	case 3:
		echo $rating;
		break;
	// If the rating is 4
	case 4:
		echo $rating;
		break;
	// If the rating is 5
	case 5:
		echo $rating;
		break;
	// Else
	default:
		echo 'No rating.';
		break;
		}
	echo '&lt;/p&gt;';
	}</code></pre>
<p>Looks pretty generic huh?  Not a lot of pizazz?  That&#8217;s OK.  You can use the same image technique we used above for book recommendations.  Instead of <code> echo $rating;</code>, you can echo an image.  You&#8217;ll need to create different images for each rating.</p>
<h3>The CSS:</h3>
<p>I won&#8217;t bother with a lot of CSS in this tutorial, but I do want to share one thing.  For the custom field <strong> Key</strong> that&#8217;s named <strong> Image Class</strong>, I&#8217;ve given it a value of <strong> left</strong>.  In my &#8220;style.css&#8221; file, I float this image to the left so that the following text will wrap around it.  Here&#8217;s the code I use:</p>
<pre><code>.left {
	float: left;
	margin: 0 10px 5px 0;
	}</code></pre>
<h3>What does it look like?</h3>
<p>Here&#8217;s an image of what my review looks like:</p>
<p><img src="http://justintadlock.com/wp-content/uploads/2007/11/book-reviews-2.gif" alt="Using WordPress custom fields to create a review management system that works" class="center i400x300" /></p>
<h3>The end of part 2 of the tutorial:</h3>
<p>You can arrange each element how you like.  You can put all the review information after the post content, before the post content, before the image, or wherever.  Do what you like.  This is the time when you should tinker with how you want your reviews to look on the screen.  Add CSS classes to things to style them how you want.  Add extra custom fields for other useful bits of information that you&#8217;d like to add.</p>
<p>In the next part of the tutorial, I&#8217;ll cover what I call the &#8220;cool&#8221; stuff.  I&#8217;ll show you how you can use these custom fields outside of home.php and single.php to create a more unique blog.</p>
<p>If you&#8217;ve noticed any errors in the code, please be sure to let me know.  <a href="http://feeds.feedburner.com/justintadlock" title="Subscribe to the feed"> Subscribe to the feed</a> to know when the next part of the tutorial rolls around.  Feel free to ask any questions and/or suggest anything you&#8217;d like to see added to the tutorial.</p>
]]></content:encoded>
			<wfw:commentRss>http://justintadlock.com/archives/2007/11/20/wordpress-custom-fields-book-reviews-pt-2/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>WordPress Custom Fields: Book Reviews Pt. 1</title>
		<link>http://justintadlock.com/archives/2007/11/15/wordpress-custom-fields-book-reviews-pt-1</link>
		<comments>http://justintadlock.com/archives/2007/11/15/wordpress-custom-fields-book-reviews-pt-1#comments</comments>
		<pubDate>Thu, 15 Nov 2007 22:51:06 +0000</pubDate>
		<dc:creator>Justin Tadlock</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Custom Fields]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://justintadlock.com/archives/2007/11/15/wordpress-custom-fields-book-reviews-pt-1</guid>
		<description><![CDATA[Finally, another part in the WordPress custom fields tutorial series. This time, weâ€™re stepping it up a notch though. Weâ€™re going to take custom fields to another level. I hope youâ€™re prepared. Weâ€™ve covered some basic things thus far, and thatâ€™s all you need to know to be prepared. Have you ever wanted to write [...]]]></description>
			<content:encoded><![CDATA[<p>Finally, another part in the <a href="/attachment/678" title="WordPress Custom Field Series"> WordPress custom fields tutorial series</a>. This time, weâ€™re stepping it up a notch though. Weâ€™re going to take custom fields to another level. I hope youâ€™re prepared. Weâ€™ve covered some basic things thus far, and thatâ€™s all you need to know to be prepared.</p>
<p>Have you ever wanted to write book, movie, music, or other type of review and archive them effectively, add extra information, and make reviewing them a little cooler? In this part of the series, Iâ€™ll cover many ways to accomplish this, while leaving you with many options to add your own twist to it. Iâ€™m using book reviews as an example, but you can easily change this to any type of review.</p>
<p>This is actually a series within a series. There will be several parts to the using custom fields to create book reviews entry into the overall series.</p>
<p>Now, thatâ€™s enough of an introduction. Letâ€™s get on with putting this thing together.</p>
<p>Today, weâ€™ll set up the code that weâ€™ll be using later. This is a little bit of a refresher course on the basics of custom fields.</p>
<h3>The setup:</h3>
<p>The first thing you need to do is create a new post. Iâ€™m going to review the book <em> I, Robot</em> by Isaac Asimov. You can find the particular version Iâ€™m using at its <a href="http://www.amazon.com/gp/product/0553803700" title="I, Robot by Isaac Asimov"> Amazon page</a>. Choose anything youâ€™d like. Now, add a new category to this post named â€œBook Reviews.â€</p>
<p>Write your review, add your tags, and whatever else you might normally do for your posts. Now, weâ€™ll create our custom fields.</p>
<h3>The custom fields:</h3>
<p>Weâ€™ll use several different custom fields for this tutorial, but you can add as many as youâ€™d like to customize your reviews. The custom field <strong> Keys</strong> that weâ€™ll create are these: Author, Book Title, Image, Image Alt, Image Class, Book Link, Published Date, and Recommend. (Remember that custom field <strong> Keys</strong> are case-sensitive.)</p>
<p>This is what your setup should look like:</p>
<ul>
<li><strong><strong> Key</strong>:</strong> Value</li>
<li><strong>Author:</strong> Author of the book</li>
<li><strong>Book Title:</strong> Title of the book</li>
<li><strong>Image:</strong> URL of an image of the book</li>
<li><strong>Image Alt:</strong> Alt text for the image</li>
<li><strong>Image Class:</strong> Use a class for personal styling of image</li>
<li><strong>Book Link:</strong> Link to an outside source for the book (maybe an Amazon page)</li>
<li><strong>Published Date:</strong> Date the book was published</li>
<li><strong>Recommend:</strong> Write Yes or No on whether you recommend this book</li>
</ul>
<p>Hereâ€™s an image of what my custom field setup looks like:</p>
<p><a href="http://justintadlock.com/wp-content/uploads/2007/11/books.gif" title="Book Reviews: Using WordPress Custom Fields"><img class="center i400x150" src="http://justintadlock.com/wp-content/uploads/2007/11/books-small.gif" alt="Using WordPress custom fields to create book reviews" /></a></p>
<p>So, you should upload an image of your book to follow this tutorial. Now, our review is written and our custom fields are ready.</p>
<h3>The code:</h3>
<p>Today, weâ€™re only going to pull the custom fields from the database and make sure everything is working correctly. Thereâ€™ll be no styling or anything super-cool, but weâ€™ll get the basics out of the way. Open index.php (you should also use this code in single.php).</p>
<p>Under this code:</p>
<pre><code>&lt;?php while(have_posts()) : the_post(); ?&gt;</code></pre>
<p>Add this:</p>
<pre><code>&lt;?php
// The book image
$image = get_post_meta($post-&gt;ID, 'Image', $single = true);

// The image alt text
$image_alt = get_post_meta($post-&gt;ID, 'Image Alt', $single = true);

// Image class (used for alignment, sizing, or styling using CSS)
$image_class = get_post_meta($post-&gt;ID, 'Image Class', $single = true);

// Book Author
$author = get_post_meta($post-&gt;ID, 'Author', $single = true);

// Outside link for book
$book_link = get_post_meta($post-&gt;ID, 'Book Link', $single = true);

// Title of the book
$book_title = get_post_meta($post-&gt;ID, 'Book Title', $single = true);

// Recommend? Yes or No
$recommend = get_post_meta($post-&gt;ID, 'Recommend', $single = true);

// Published date of book
$published_date = get_post_meta($post-&gt;ID, 'Published Date', $single = true);
?&gt;</code></pre>
<p>Basically, what this does is pull each custom field from the database and set it to a PHP variable. This is important because weâ€™ll use the variable a lot in the future, and we want to keep the code as easy to manage as possible. The shorter variable names makes this possible.</p>
<p>Now, above this code:</p>
<pre><code>&lt;?php the_content(); ?&gt;</code></pre>
<p>Add this:</p>
<pre><code>&lt;?php
// If there's an image
if($image !== '') {
	echo '&lt;p&gt;&lt;strong&gt;Image URL:&lt;/strong&gt; ' . $image . '&lt;/p&gt;';
	}

// If there's image alt text
if($image_alt !== '') {
	echo '&lt;p&gt;&lt;strong&gt;Image Alt:&lt;/strong&gt; ' . $image_alt . '&lt;/p&gt;';
	}

// If there's an image CSS class
if($image_class !== '') {
	echo '&lt;p&gt;&lt;strong&gt;Image Class:&lt;/strong&gt; ' . $image_class . '&lt;/p&gt;';
	}

// If there's a book title
if($book_title !== '') {
	echo '&lt;p&gt;&lt;strong&gt;Title:&lt;/strong&gt; ' . $book_title . '&lt;/p&gt;';
	}

// If there's a book link
if($book_link !== '') {
	echo '&lt;p&gt;&lt;strong&gt;Book Link:&lt;/strong&gt; ' . $book_link . '&lt;/p&gt;';
	}

// If there's a published date
if($published_date !== '') {
	echo '&lt;p&gt;&lt;strong&gt;Published:&lt;/strong&gt; ' . $published_date . '&lt;/p&gt;';
	}

// If there's an author
if($author !== '') {
	echo '&lt;p&gt;&lt;strong&gt;Author:&lt;/strong&gt; ' . $author . '&lt;/p&gt;';
	}

// If there's a recommendation
if($recommend == 'yes' || $recommend == 'Yes') {
	echo '&lt;p&gt;&lt;strong&gt;Recommend?&lt;/strong&gt; ' . $recommend . '&lt;/p&gt;';
	}
elseif($recommend == 'no' || $recommend == 'No') {
	echo '&lt;p&gt;&lt;strong&gt;Recommend?&lt;/strong&gt; ' . $recommend . '&lt;/p&gt;';
	}
else { echo ''; }
?&gt;</code></pre>
<p>This will give you a list of the various custom fields that youâ€™ve created. I hope you can understand a little bit about whatâ€™s happening behind the scenes here by looking at the a text display of your custom fields.</p>
<p>Hereâ€™s what the output on my page looks like:</p>
<p><img src="http://justintadlock.com/wp-content/uploads/2007/11/books-2.gif" alt="View of the outputted page by using WordPress custom fields to create book reviews" class="center i400x300" /></p>
<p>Thatâ€™s the end of this part of the tutorial. I want you to start thinking about what other <strong> Keys</strong> you might want to add. Do you want to add a rating system? Hardback and/or paperback release dates? Links to other outside sources? There are limitless options here.</p>
<p>Some other things Iâ€™ll cover are putting this all together, creating pretty archives, a page for all your book reviews, and other various uses.</p>
<p>Please share your thoughts and <a href="http://feeds.feedburner.com/justintadlock" title="Subscribe to the feed"> subscribe to the feed</a> to know when the next part of the tutorial comes out.</p>
]]></content:encoded>
			<wfw:commentRss>http://justintadlock.com/archives/2007/11/15/wordpress-custom-fields-book-reviews-pt-1/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>WordPress Custom Fields: Listing A Series Of Posts</title>
		<link>http://justintadlock.com/archives/2007/11/01/wordpress-custom-fields-listing-a-series-of-posts</link>
		<comments>http://justintadlock.com/archives/2007/11/01/wordpress-custom-fields-listing-a-series-of-posts#comments</comments>
		<pubDate>Thu, 01 Nov 2007 20:55:42 +0000</pubDate>
		<dc:creator>Justin Tadlock</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Custom Fields]]></category>

		<guid isPermaLink="false">http://justintadlock.com/archives/2007/11/01/wordpress-custom-fields-listing-a-series-of-posts</guid>
		<description><![CDATA[This is an updated version of this article. If you&#8217;re looking for the older version, see page 2. Do you write long series of posts? Do you have a bunch of articles that definitely need to be linked together? This WordPress custom fields technique will allow you to do that without a lot of hassle. [...]]]></description>
			<content:encoded><![CDATA[<p class="note">This is an updated version of this article.  If you&#8217;re looking for the older version, see page 2.</p>
<p><span class="drop-cap">D</span>o you write long series of posts?  Do you have a bunch of articles that definitely need to be linked together?  This WordPress custom fields technique will allow you to do that without a lot of hassle.  Basically, this is a simple, customized related posts script.  It checks to see whether a post is in a series, then lists all the posts in the series.</p>
<p>The WordPress custom fields tutorial series uses this code for listing posts.  You can scroll to the bottom of this entry to see what the outcome looks like.</p>
<p>The reason I&#8217;m writing this tutorial is precisely because of the WordPress custom fields series.  After three posts, and going back to each previous post to add a link to the new post, I realized that this was only going to get more tedious and time-consuming each time I wrote another article in the series.  So, this idea was born from that.</p>
<h2>How to set up your posts</h2>
<p>You&#8217;ll probably want more than one post.  This is a series after all.</p>
<p>Create a custom field <strong>key</strong> called <code>Series</code> first.  Give it a unique custom field <strong>value</strong>.  You should give every article in the series this exact same key and value.</p>
<p>This is all you have to do on the <em>Write Post</em> screen.</p>
<h2>Adding the series function</h2>
<p>Open your theme&#8217;s <code>functions.php</code> file and paste this code into it.</p>
<pre><code>&lt;?php
function article_series() {
	global $post;
	$series = get_post_meta($post-&gt;ID, 'Series', true);
	if($series) :
		$args = array(
			'numberposts' =&gt; -1,
			'meta_key' =&gt; 'Series',
			'meta_value' =&gt; $series,
		);
		$series_posts = get_posts($args);
		if($series_posts) :
			$class = preg_replace("/[^a-z0-9\\040\\.\\-\\_\\\\]/i", "", $series);
			$class = strtolower(str_replace(array(' ', '&amp;nbsp;'), '-', $class));
			echo '&lt;div class="series series-' . $class . '"&gt;&lt;h4 class="series-title"&gt;' . __('Articles in this series') . '&lt;/h4&gt;&lt;ul&gt;';
			foreach($series_posts as $serial) :
				if($serial-&gt;ID == $post-&gt;ID)
					echo '&lt;li class="current-post"&gt;' . $serial-&gt;post_title . '&lt;/li&gt;';
				else
					echo '&lt;li&gt;&lt;a href="' . get_permalink($serial-&gt;ID) . '" title="' . str_replace('"', '&quot;', $serial->post_title) . '"&gt;' . str_replace('"', '&quot;', $serial->post_title) . '&lt;/a&gt;&lt;/li&gt;';
			endforeach;
			echo '&lt;/ul&gt;&lt;/div&gt;';
		endif;
	endif;
}
?&gt;</code></pre>
<h2>Add your function to your single post template</h2>
<p>You can add this to <code>single.php</code> and/or <code>page.php</code>.  If your theme has a hook after posts/pages, you can even hook it to that with without touching a template file (ask your theme author about this).</p>
<p>So, open your file and add this somewhere in it before the call for <code>comments_template()</code>, preferrably somewhere within the Loop.</p>
<pre><code>&lt;?php article_series(); ?&gt;</code></pre>
<p>That&#8217;s it, and you&#8217;re done.  Just save everything and check out your series list at the end of posts within that series.  You can create as many of these as you like.</p>
<h2>Final thoughts</h2>
<p>Now, that should do it.  You can always tweak this script to better apply to your needs.  There are also some other uses you can extract from this article.</p>
<p>I&#8217;m already loving this because I won&#8217;t have to go back to the previous three posts in this series just to link back to this article.  Life is much simpler now.</p>
<p>As always, remember to <a href="http://feeds.feedburner.com/justintadlock" title="Subscribe to the feed"> subscribe to the feed</a> to stay updated on the WordPress custom fields tutorial series.  I have a few more ideas up my sleeve and am ready to post them as soon as I finish testing.</p>
<p>Let me know if you liked this tutorial.  I&#8217;m always open to feedback and looking for new ideas to implement.</p>
]]></content:encoded>
			<wfw:commentRss>http://justintadlock.com/archives/2007/11/01/wordpress-custom-fields-listing-a-series-of-posts/feed</wfw:commentRss>
		<slash:comments>50</slash:comments>
		</item>
		<item>
		<title>WordPress Custom Fields: Adding Images To Posts</title>
		<link>http://justintadlock.com/archives/2007/10/27/wordpress-custom-fields-adding-images-to-posts</link>
		<comments>http://justintadlock.com/archives/2007/10/27/wordpress-custom-fields-adding-images-to-posts#comments</comments>
		<pubDate>Sat, 27 Oct 2007 07:37:07 +0000</pubDate>
		<dc:creator>Justin Tadlock</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Custom Fields]]></category>
		<category><![CDATA[WordPress Tutorials]]></category>

		<guid isPermaLink="false">http://justintadlock.com/archives/2007/10/27/wordpress-custom-fields-adding-images-to-posts</guid>
		<description><![CDATA[This tutorial will explain how to add images to your posts using WordPress custom fields. It is the third part of the Using WordPress Custom Fields Series. This tutorial assumes you at least know what XHTML, PHP, CSS, and WordPress are. Plus, you should know how to input a custom field &#8220;Key&#8221; and &#8220;Value&#8221; and [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://justintadlock.com/wp-content/uploads/2007/10/custom-field-post-images.gif" alt="Using WordPress custom fields to add images to your posts like DoshDosh.com" title="Using WordPress custom fields to add images to your posts like DoshDosh.com" class="thumbnail" /></p>
<p>This tutorial will explain how to add images to your posts using WordPress custom fields.  It is the third part of the <a href="http://justintadlock.com/archives/2007/10/24/using-wordpress-custom-fields-introduction" title="Using WordPress Custom Fields Series"> Using WordPress Custom Fields Series</a>.  This tutorial assumes you at least know what XHTML, PHP, CSS, and WordPress are.  Plus, you should know how to input a custom field &#8220;Key&#8221; and &#8220;Value&#8221; and <a href="http://codex.wordpress.org/Using_Image_and_File_Attachments" title="Using image and file attachments"> upload images</a>.</p>
<p>The example I&#8217;m basing this tutorial off of is <a href="http://doshdosh.com" title="Dosh Dosh"> Dosh Dosh</a>.  If you notice, he adds an avatar-sized image to each post.  I don&#8217;t know whether he uses a technique like I&#8217;m about to explain or adds each image to the &#8220;Post&#8221; box.</p>
<h3>Why use custom fields to add images to posts?</h3>
<p>There are two main reasons for this.  Typically, when a blogger adds an image to a post, he or she will simply upload the photo and add the picture in the posting textarea.</p>
<ol>
<li>You can display images when using excerpts.</li>
<li>You can pull the image out from just about anywhere and use it how you like.</li>
</ol>
<p>Most WordPress theme archive, category, and search templates display post excerpts, and even some home pages.  Why not display a custom image for each post?  This technique will allow you to do that.</p>
<p>If you take a look at my WordPress Themes page, you can see how I pull an image out of a custom field.  Of course, I created a plugin with some added features for that particular page.  But, it shows you how information can be pulled out of custom fields to be used elsewhere on the blog.</p>
<h3>Upload images and create Keys and Values:</h3>
<p>The first thing you need to do is upload an image.  For the sake of this tutorial, I will assume these images are thumbnailed-sized, such as ones that you might see on a message board or at Dosh Dosh.  You can, of course, do as you like when it comes to sizing your images.</p>
<p>Now, create a Key named &#8220;Thumbnail.&#8221;  Give it a Value of the URL of the image you uploaded (ex: /wp-content/uploads/image.gif).</p>
<p>Create a key named &#8220;Thumbnail Class.&#8221;  Give it a Value of &#8220;thumbnail-class&#8221; or anything that you can use to style in your stylesheet (style.css).</p>
<p>You can also create alt text for your image.  This is optional.  Create a Key named &#8220;Thumbnail Alt&#8221; with a Value of &#8220;Describe your image.&#8221;  If you don&#8217;t do this, the alt text will default to the post title.</p>
<h3>Now, for the code:</h3>
<p>You can use this code in any of your templates (index.php, single.php, category.php, archive.php, etc.).  Open one of those files in your text editor of choice (I&#8217;m editing index.php in Notepad).</p>
<p>After:</p>
<pre><code>if(have_posts()) : while(have_posts()) : the_post();</code></pre>
<p>Add this:</p>
<pre><code>// check for thumbnail
$thumb = get_post_meta($post-&gt;ID, 'Thumbnail', $single = true);
// check for thumbnail class
$thumb_class = get_post_meta($post-&gt;ID, 'Thumbnail Class', $single = true);
// check for thumbnail alt text
$thumb_alt = get_post_meta($post-&gt;ID, 'Thumbnail Alt', $single = true);</code></pre>
<p>Basically, this just retrieves your custom field information for each post that will be displayed by the loop.  When it retrieves the custom field Key and Value, it assigns it to a PHP variable.  This is important because it&#8217;ll make the coding later cleaner and we only have to retrieve the information once for each post.  (<em>Also note that this is the technique I&#8217;ll use on later tutorials, unless I come up with an even better way to accomplish this.</em>)</p>
<p>Before (in the same file):</p>
<pre><code>&lt;?php the_content();  ?&gt;</code></pre>
<p>or</p>
<pre><code>&lt;?php the_excerpt();  ?&gt;</code></pre>
<p>Add this code:</p>
<pre><code>&lt;?php // if there's a thumbnail
if($thumb !== '') { ?&gt;
	&lt;p&gt;
	&lt;img src="&lt;?php echo $thumb; ?&gt;"
	class="&lt;?php if($thumb_class !== '') { echo $thumb_class; } else { echo "left"; } ?&gt;"
	alt="&lt;?php if($thumb_alt !== '') { echo $thumb_alt; } else { echo the_title(); } ?&gt;"
	/&gt;
	&lt;/p&gt;
&lt;?php } // end if statement

// if there's not a thumbnail
else { echo ''; } ?&gt;</code></pre>
<p>This code outputs the image right before your content is displayed.  Pretty simple, huh?  You can obviously add other things like width and height using custom fields or set a width and height for all images.  What I will show you next is how to do this with your &#8220;thumbnail-class&#8221; instead.</p>
<p>Open style.css file from your theme folder and add this code:</p>
<pre><code>.thumbnail-class {
	float: left;
	width: 100px;
	height: 100px;
	margin: 0 15px 0 0;
	}
.left {
	float: left;
	margin: 0 15px 0 0;
	}</code></pre>
<p>The class &#8220;left&#8221; is the default in case you forget to input a &#8220;Thumbnail Class&#8221; custom field Key.  Obviously, how to style your image is up to you.  Play around with it, have some fun, and see what you can come up with.  You can also do away with the entire &#8220;Thumbnail Class&#8221; custom field and just input the class in the XHTML.  I prefer to leave &#8220;Thumbnail Class&#8221; open, in case I want to style or align individual thumbnails differently.</p>
<p>That&#8217;s pretty much it.  You can do a lot with this.  For example:</p>
<ul>
<li>Add a link around the displayed thumbnail to a larger image, another page, or the single.php page.</li>
<li>List posts by just their image.</li>
<li>Creat kick-ass archive and search pages.</li>
</ul>
<p>As I&#8217;ve said before regarding custom fields, their only limit is your imagination.  Follow this tutorial series by <a href="http://feeds.feedburner.com/justintadlock" title="Subscribe to the feed"> subscribing to the feed</a>.  If you have anything you would like to see done and think it might be possible with custom fields, let me know because I&#8217;m always looking for more things to play around with.</p>
]]></content:encoded>
			<wfw:commentRss>http://justintadlock.com/archives/2007/10/27/wordpress-custom-fields-adding-images-to-posts/feed</wfw:commentRss>
		<slash:comments>120</slash:comments>
		</item>
		<item>
		<title>Using WordPress Custom Fields: Introduction</title>
		<link>http://justintadlock.com/archives/2007/10/24/using-wordpress-custom-fields-introduction</link>
		<comments>http://justintadlock.com/archives/2007/10/24/using-wordpress-custom-fields-introduction#comments</comments>
		<pubDate>Wed, 24 Oct 2007 21:03:08 +0000</pubDate>
		<dc:creator>Justin Tadlock</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Custom Fields]]></category>

		<guid isPermaLink="false">http://justintadlock.com/archives/2007/10/24/using-wordpress-custom-fields-introduction</guid>
		<description><![CDATA[I decided a couple of weeks ago that I wanted to do an ongoing tutorial series with WordPress custom fields. At first, I thought I&#8217;d simply write a complete guide, but that idea seemed daunting. It seems far easier to break down the guide into individual posts in a series. Instead of writing a general [...]]]></description>
			<content:encoded><![CDATA[<p>I decided a couple of weeks ago that I wanted to do an ongoing tutorial series with WordPress custom fields.  At first, I thought I&#8217;d simply write a complete guide, but that idea seemed daunting.  It seems far easier to break down the guide into individual posts in a series.  Instead of writing a general guide, I will write detailed tutorials on specific and practical uses for custom fields.</p>
<p>I&#8217;ve been using custom fields for various tasks on this site for a while.  A few places you can see some custom field uses on this site are:</p>
<ul>
<li>On the <a href="http://justintadlock.com/archives/2007/03/18/belief-o-matic" title="Reading Page"> Reading</a> page, you can see all of my book reviews pulled into a nice organized list.  Under each book&#8217;s title is the author&#8217;s name.</li>
<li>My <a href="http://justintadlock.com/buffy-angel" title="Projects Page"> Projects</a> page, there is a list of several subpages.  For each subpage, there is an image, title, and description.  This list of subpages is made possible by custom fields.</li>
</ul>
<p>These are fairly simple examples, and this tutorial series will cover more advanced uses later in the series.  Mostly, I will cover what I think are practical uses of custom fields.  Of course, suggestions are always welcome.  So, if you want something specific, but can&#8217;t figure out how to do it, don&#8217;t hesitate to ask.  This is a learning experience just as much for me as it is for you.</p>
<h3>What are custom fields?</h3>
<p>Custom fields are additional information that you can add to your posts and pages.</p>
<p>The good starting point is checking out WordPress&#8217; <a href="http://codex.wordpress.org/Using_Custom_Fields" title="Using Custom Fields: WordPress Codex"> Using Custom Fields</a>.  It&#8217;s a very basic tutorial, but will give you some idea of what custom fields are.  The Undersigned has a <a href="http://theundersigned.net/2006/09/wordpress-how-to-custom-fields" title="WordPress: How-to Custom Fields"> good tutorial</a> also.  Neither of these tutorials cover a lot of uses, and I think that&#8217;s why many people don&#8217;t know what to do with custom fields &mdash; there are not enough detailed tutorials.</p>
<p>The truth is that <em> you can do pretty much anything you want with custom fields</em>.  Your imagination is your limit.</p>
<h3>Basic Usage:</h3>
<p>The first thing you need to know is where to input custom fields.  On &#8220;Write Page&#8221; or &#8220;Write Post&#8221; in the WordPress Dashboard, there is section at the bottom of the page that lets you input as many custom fields as you want.  Here is a screenshot:</p>
<p><a href='http://justintadlock.com/wp-content/uploads/2007/10/custom-fields.gif' title='View of the WordPress custom field box in the dashboard'><img class='center 425x150' src='http://justintadlock.com/wp-content/uploads/2007/10/custom-fields-small.gif' alt='View of the WordPress custom field box in the admin panel' /></a></p>
<p>There are two different things you can input into a custom field &mdash; &#8220;Key&#8221; and &#8220;Value.&#8221;  It doesn&#8217;t get much simpler than that.</p>
<p><em>What it is a Key and Value?</em>  The Key is the name of the custom field and the Value is simply the value you want to give to that name.  For example, you might write book reviews and want to add some additional information like the book&#8217;s author or  publisher, give it a link through your Amazon Affiliate membership, create a star rating system with images, or a multitude of other bits and pieces.</p>
<p><em>What do I do with this information?</em>  You can pull this information out in posts, on other pages, into sidebars, or wherever.</p>
<p>To give a list of your book reviews in alphabetical order with the author&#8217;s name, you could create a custom field with a Key named &#8220;author&#8221; and a Value of &#8220;The Author&#8217;s Name&#8221; for several posts, or reviews.  After writing these reviews and putting them into your &#8220;Book Reviews&#8221; category, you would put together a page called &#8220;Book Reviews&#8221; or create a category-specific template for your &#8220;Book Reviews&#8221; category.</p>
<p>Here&#8217;s how you would list each book review (category=27 would need to be changed to your &#8220;Book Reviews&#8221; category ID):</p>
<pre><code>&lt;ul&gt;
&lt;?php
$book_reviews = get_posts('category=27&#038;numberposts=-1&#038;orderby=post_name&#038;order=ASC');
foreach($book_reviews as $post) : setup_postdata($post);
?&gt;
&lt;li&gt;
&lt;a href="&lt;?php the_permalink(); ?&gt;" title="&lt;?php the_title(); ?&gt;"&gt;&lt;?php the_title(); ?>&lt;/a&gt;
&lt;?php $author = get_post_meta($post->ID, "author", $single = true);
	if($author !== '') {
	echo 'by ' . $author;
	} ?&gt;
&lt;/li&gt;
&lt;?php endforeach; ?&gt;
&lt;/ul&gt;</code></pre>
<p>This is pretty basic, but I hope you&#8217;re getting some kind of an idea what custom fields are and what you might can do with them.  I don&#8217;t want to give too much away on the Book Review use yet because I will definitely write a full-blown tutorial on how to add cool information to reviews.</p>
<p>Now, that you&#8217;re warmed up a bit, stay tuned by <a href="http://feeds.feedburner.com/justintadlock" title="Subscribe to the feed"> subscribing to the feed</a> or bookmarking this page for future reference.  I will link all the articles in the custom field tutorial series for WordPress here.</p>
]]></content:encoded>
			<wfw:commentRss>http://justintadlock.com/archives/2007/10/24/using-wordpress-custom-fields-introduction/feed</wfw:commentRss>
		<slash:comments>113</slash:comments>
		</item>
		<item>
		<title>Using WordPress Custom Fields: Subtitles</title>
		<link>http://justintadlock.com/archives/2007/10/16/using-wordpress-custom-fields-subtitles</link>
		<comments>http://justintadlock.com/archives/2007/10/16/using-wordpress-custom-fields-subtitles#comments</comments>
		<pubDate>Tue, 16 Oct 2007 18:55:33 +0000</pubDate>
		<dc:creator>Justin Tadlock</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Custom Fields]]></category>
		<category><![CDATA[SEO]]></category>

		<guid isPermaLink="false">http://justintadlock.com/archives/2007/10/16/using-wordpress-custom-fields</guid>
		<description><![CDATA[This article, or series of articles, is long overdue. I&#8217;ve been using custom fields to make &#8220;little&#8221; fixes around this site for a while now. They are probably the most powerful feature of WordPress, yet the least used. Maybe that&#8217;s because they&#8217;re hidden away at the bottom of the &#8220;Write&#8221; page. I want to share [...]]]></description>
			<content:encoded><![CDATA[<p>This article, or series of articles, is long overdue.  I&#8217;ve been using custom fields to make &#8220;little&#8221; fixes around this site for a while now.  They are probably the most powerful feature of WordPress, yet the least used.  Maybe that&#8217;s because they&#8217;re hidden away at the bottom of the &#8220;Write&#8221; page.  I want to share some of the things I&#8217;ve learned with you.  Maybe I&#8217;ll even learn a thing or two while doing it.</p>
<p>This is the first post in what I hope to be an ongoing series of how to use <a href="http://codex.wordpress.org/Using_Custom_Fields" title="WordPress: Using Custom Fields"> WordPress&#8217; custom fields</a>.  The inspiration for finally getting my butt in gear and writing the first post was Nick La&#8217;s post, <a href="http://www.webdesignerwall.com/tutorials/wordpress-theme-hacks" title="WordPress Theme Hacks"> WordPress Theme Hacks</a>.</p>
<p>This post will serve as a quick tutorial on a simple use of custom fields.  I will later get more in depth with the &#8220;why&#8221; and &#8220;how&#8221; of using these things.  So, to get your feet a little wet&#8230;</p>
<p>Today, I will show a simple solution to fixing your title tags (those words at the top of your Web browser when you visit a site).  This will also help a little in optimizing your site for SEO.  Basically, we&#8217;re going to create a subtitle to each post or page we create.  This subtitle will be displayed with the title at the top of the browser.</p>
<p>The first thing you need to do is open &#8220;header.php.&#8221;  Simple enough, right?  Find:</p>
<pre><code>&lt;title&gt;
&lt;/title&gt;</code></pre>
<p>We will only be working between the title tags today.</p>
<p>Nick gives us a nice starting point in fixing your title tags.  Here is the code:</p>
<pre><code>&lt;?php
if (is_home()) {
	echo bloginfo('name');
} elseif (is_404()) {
	echo '404 Not Found';
} elseif (is_category()) {
	echo 'Category:'; wp_title('');
} elseif (is_search()) {
	echo 'Search Results';
} elseif (is_day() || is_month() || is_year() ) {
	echo 'Archives:'; wp_title('');
} elseif (is_tag()) {
	echo 'Tag:'; wp_title('');
} else {
	echo wp_title('');
}
?&gt;</code></pre>
<p>Obviously, you can play around with this a little and do what you like with it.  Now, we will figure out how to optimize for SEO by inputting extra information with custom fields.</p>
<p>Save your copy of &#8220;header.php.&#8221;  Open your WordPress dashboard and go to &#8220;Write Post&#8221; or &#8220;Write Page.&#8221;  After writing your post, scroll down to the box labeled &#8220;Custom Fields.&#8221;  Add a new &#8220;Key&#8221; named &#8220;subtitle.&#8221;  Write your subtitle in the &#8220;Value&#8221; box.</p>
<p><a href='http://justintadlock.com/blog/wp-content/uploads/2007/10/custom-fields-subtitle.gif' title='How to input a subtitle into the custom field box for use with the title tag'><img class='center i400x150' src='http://justintadlock.com/blog/wp-content/uploads/2007/10/cf-subtitle-thumb.gif' alt='How to input a subtitle into the custom field box for use with the title tag' /></a></p>
<p>Now, you are almost ready to see your efforts in all their beauty.  Open &#8220;header.php&#8221; again (it should still be open).  Below the last <code> echo wp_title('');</code> add the following code on the next line.</p>
<pre><code>$subtitle = get_post_meta
($post->ID, 'subtitle', $single = true);
if($subtitle !== '') echo ': ' . $subtitle;</code></pre>
<p>This code pulls your custom field key of &#8220;subtitle&#8221; out and displays it on whatever pages have a subtitle.  Your final code should look like this:</p>
<pre><code>&lt;?php
if (is_home()) {
	echo bloginfo('name');
} elseif (is_404()) {
	echo '404 Not Found';
} elseif (is_category()) {
	echo 'Category:'; wp_title('');
} elseif (is_search()) {
	echo 'Search Results';
} elseif (is_day() || is_month() || is_year() ) {
	echo 'Archives:'; wp_title('');
} elseif (is_tag()) {
	echo 'Tag:'; wp_title('');
} else {
	echo wp_title('');
	$subtitle = get_post_meta
	($post->ID, 'subtitle', $single = true);
	if($subtitle !== '') echo ': ' . $subtitle;
}
?&gt;</code></pre>
<p>Yes, simple, yet useful.  You don&#8217;t need a plugin for this task.  You can see how I&#8217;m putting it to use on this page.  The title is &#8220;Using WordPress Custom Fields,&#8221; and the subtitle is &#8220;A series of tutorials on WordPress&#8217; most powerful feature.&#8221;</p>
<p>I hope you continue to follow this series, as I hope to continue to learn and develop.  <a href="http://feeds.feedburner.com/justintadlock" title="Subcribe to Justin Tadlock"> Subscribe to my feed</a> to keep updated or bookmark this page because I&#8217;ll provide a link here to the next tutorial.  In the next article, I will talk more about what custom fields are and why we need to use them.</p>
<p>Do you have any ideas on what would make a useful function using custom fields, but you&#8217;re just not sure how to implement it?</p>
]]></content:encoded>
			<wfw:commentRss>http://justintadlock.com/archives/2007/10/16/using-wordpress-custom-fields-subtitles/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Cleaning Up Your WordPress Blog &amp; Other Extreme Sports</title>
		<link>http://justintadlock.com/archives/2007/09/30/cleaning-up-your-wordpress-blog-other-extreme-sports</link>
		<comments>http://justintadlock.com/archives/2007/09/30/cleaning-up-your-wordpress-blog-other-extreme-sports#comments</comments>
		<pubDate>Sun, 30 Sep 2007 13:33:27 +0000</pubDate>
		<dc:creator>Justin Tadlock</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Blogging]]></category>
		<category><![CDATA[Bookmarks]]></category>
		<category><![CDATA[Custom Fields]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[WordPress Tips]]></category>

		<guid isPermaLink="false">http://justintadlock.com/archives/2007/09/30/cleaning-up-your-wordpress-blog-other-extreme-sports</guid>
		<description><![CDATA[With the summer ending and the autumn chill in the air, which is actually quite nice, I felt that it was time to do some major housecleaning. No, not by breaking out the mop and broom&#8212;but my brain, fingers, keyboard, and sheer force of will (I should probably break out the mop and broom too, [...]]]></description>
			<content:encoded><![CDATA[<p>With the summer ending and the autumn chill in the air, which is actually quite nice, I felt that it was time to do some major housecleaning.  No, not by breaking out the mop and broom&mdash;but my brain, fingers, keyboard, and sheer force of will (I should probably break out the mop and broom too, but I&#8217;ll save that for another day).</p>
<p>So, you wanna clean up your WordPress blog?  Here are six tips that I think might help.  These are recent updates to my blog that have motivated me to get back to blogging on a regular schedule.</p>
<ol class="space">
<li>
<h3>A New WordPress Theme</h3>
<p>If you haven&#8217;t updated your theme in a while, say for at least the last six months or year, maybe your look&#8217;s getting a little stale.  Since I haven&#8217;t updated my theme since <a href="http://justintadlock.com/archives/2007/03/30/back-from-spring-break-a-new-theme" title="Back From Spring Break &amp; A New Theme"> March</a>, I felt it was time for something fresh.  My regular readers will automatically notice the new look.  Do you like it?  I call it Harmony because I was trying to balance dark and light.</p>
<p>Maybe you don&#8217;t want to scare off anyone with a completely new theme.  You could possibly tweak some of the files, add something extra, to your blog.  It&#8217;s always best to freshen things up a little every now and then.  Change the colors, font size, or header image.</p>
</li>
<li>
<h3>Unclutter Your Blogroll/Links</h3>
<p>This section can easily get unruly if you don&#8217;t pay careful attention to it.  Before I dared touch my <a href="http://justintadlock.com/links" title="Links To Other Websites and Blogs"> links page</a>, I cleaned my favorites/bookmarks folder on my computer.</p>
<p>Get rid of those sites that you never visit anymore.  Remove sites that no longer exist.  I finally just started erasing everything.  It felt liberating.  Your site have music in the background?  Delete.  8,000 icons in your sidebar?  Delete.  Haven&#8217;t updated in the last few months?  Layout looks like something from a six-year-old&#8217;s geocities page?  Delete.  Delete.  (I must point out that after a while, I almost went insane.  Several times, my browser crashed because of the blogs I was going through.  Yes, those sites were deleted.)</p>
<p>After organizing my favorites, I easily updated my <a href="http://justintadlock.com/links" title="Links To Other Websites &amp; Blogs"> links</a>.  I added all the links from my favorites and deleted those that were no longer of use to me.  I made two categories:  Blogroll and Dailies.  You might want to organize these in another manner, but some kind of organization is key.</p>
</li>
<li>
<h3>Clean Out Your Plugins Folder</h3>
<p>I probably had 50 plugins in my plugins folder while using less than 10 (I do a lot of testing).  With the release of <a href="http://wordpress.org/development/2007/09/wordpress-23" title="WordPress 2.3"> WordPress 2.3</a>, this shot to the top of my to-do list.</p>
<p>The new tags feature allowed me to get rid of Bunny&#8217;s Technorati Tags.  Canonical URLs and Pretty URLs let me delete the No WWW and Permalink Redirect plugins.  So, you should definitely clean out those unused plugins.  Make room for the next plugins you might want to use.</p>
</li>
<li>
<h3>Add Fresh Features</h3>
<p>It&#8217;s important to keep adding new features to your blog as time passes on.  Give your readers a little something extra.</p>
<p>With the advent of the tags feature, I needed to update my theme a little.  Now on each page&#8217;s single post, I have added a tags section near the bottom of the post.  I also have a nifty little <a href="http://justintadlock.com/tags" title="Tags Page"> Tags</a> page, which is basically just a tag cloud.  I also have a new <a href="http://justintadlock.com/topics" title="Topics Page"> Topics</a> page.</p>
<p>Maybe you&#8217;ve been waiting for a moment to implement that cool, new plugin you&#8217;ve found, but haven&#8217;t gotten around to it.  That was the case when I added <a href="http://eightface.com/wordpress/flickrrss/" title="Flickr RSS Plugin"> Flickr RSS</a>, <a href="http://www.stuff.yellowswordfish.com/paged-navigation/" title="Paged Navigation Plugin"> Paged Navigation</a>, and <a href="http://wasabi.pbwiki.com/Related%20Entries" title="Related Posts Plugin"> Related Posts</a>.  The latter two gives users easier access to older content on my site, and I think that&#8217;s ultimately what I wanted to do when looking for new plugins.</p>
</li>
<li>
<h3>Learn To Use Custom Fields</h3>
<p>Custom fields are a powerful feature of WordPress, but I rarely hear of anyone using them.  You can automate so many things that you usually input every time you write a post.  Plus, there are many other cool things you can do with them.</p>
<p>I&#8217;ve always used custom fields on my <a href="http://justintadlock.com/reading" title="Reading Page: Book Reviews &amp; Reading List"> reading</a> page to return a book author&#8217;s name.  However, I wanted to do a little something more.  Look at any post in my <a href="http://justintadlock.com/topics/book-reviews" title="Book Reviews Archive"> Book Reviews</a> and you will notice a few things immediately.</p>
<p>Take, for example, my <a href="http://justintadlock.com/archives/2007/05/04/world-is-flat" title="Review of Thomas L. Friedman's The World is Flat"> Review of Thomas L. Friedman&#8217;s <em> The World is Flat</em></a>.  At the top left of the post, there is an image.  It doesn&#8217;t look like much, but there&#8217;s a lot of information along with that image.  I used custom fields to display the image, alt text, link to Amazon&#8217;s page, and size and alignment of the image.  Now, each time I write a new book review, I don&#8217;t have to write all that code.  It&#8217;s automatically generated for me.  I just input information into the custom field boxes.  If you want a detailed &#8220;how-to&#8221; article on this, I plan on writing one in the next few days.</p>
</li>
<li>
<h3>Update Your Pages</h3>
<p>We&#8217;re constantly updating our blog by writing new posts, putting fresh content out there.  But, sometimes we forget about our pages.  By definition they are &#8220;static,&#8221; and many times, users don&#8217;t return to them after the first view.  Some people never write anything more than an About and Archives page, which is all right if that&#8217;s your cup of tea.</p>
<p>I, however, have a lot of content that doesn&#8217;t belong in blogs posts.  I recently revamped the <a href="http://justintadlock.com/projects" title="Projects Page"> Projects</a> section of this blog.  I used custom fields to display an image and description of each subpage.  Sure, it&#8217;s only a new look, but it adds a little bit to the blog.</p>
<p>Whatever you decide to do, keeping new content in your pages is also important.  If users see that you rarely update pages, then they probably won&#8217;t return to them when you do actually update them.</p>
</li>
</ol>
<p>Keeping your WordPress blog updated, not just in blog posts, but in other areas as well, can sometimes be a daunting task.  However, it is something that needs to be done every now and again.  Readers like the blogger who &#8220;goes the extra mile&#8221; or &#8220;gives &#8220;110%&#8221; to their blog.</p>
<p>Updating your blog can also motivate you.  I feel like a new blogger now.  Like that first day I started my <a href="expage.com" title="Express Page"> Expage</a> almost five years ago, which was lost because their servers crashed.  I just want to write and write and write and&#8230;</p>
<p>So, give your WordPress blog the cleaning that it deserves.  Your readers will like it better.  You will feel better.  Do you have any useful tips to add?  I&#8217;d love to hear your ideas.</p>
]]></content:encoded>
			<wfw:commentRss>http://justintadlock.com/archives/2007/09/30/cleaning-up-your-wordpress-blog-other-extreme-sports/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

