<?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>RLM &#187; Custom Fields</title>
	<atom:link href="http://www.rlmseo.com/blog/category/wordpress/custom-fields/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rlmseo.com</link>
	<description></description>
	<lastBuildDate>Tue, 31 Jan 2012 03:37:47 +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>Using WordPress Custom Fields</title>
		<link>http://www.rlmseo.com/blog/wordpress-custom-fields/</link>
		<comments>http://www.rlmseo.com/blog/wordpress-custom-fields/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 13:43:52 +0000</pubDate>
		<dc:creator>jcrens8392</dc:creator>
				<category><![CDATA[Custom Fields]]></category>

		<guid isPermaLink="false">http://www.rlmseo.com/?p=581</guid>
		<description><![CDATA[Using WordPress as a CMS often for client sites I use custom fields perhaps quite a bit more than the regular WordPress user. Still, I've received quite a few requests to write about the use of custom fields in WordPress, so in this post I'll cover the ins and outs of WordPress custom fields.]]></description>
			<content:encoded><![CDATA[<p>Using <a title="Posts related to WordPress as a CMS" href="http://www.rlmseo.com/blog/category/wordpress-as-cms/">WordPress as a CMS</a> often for client sites I use custom fields perhaps quite a bit more than the regular WordPress user. Still, I&#8217;ve received quite a few requests to write about the use of custom fields in WordPress, so in this post I&#8217;ll cover the ins and outs of WordPress custom fields.</p>
<h2>What Are Custom Fields?</h2>
<p>I realize some WordPress users may not even know what custom fields are so let me clarify. The custom field panel can be found on the New/Edit post screen under the conspicuous title, &#8220;Custom Fields.&#8221; That panel is rather cryptic if you&#8217;ve never used it before, but custom fields are really simple to use and incredibly useful. A perfect example of how they can be used is the header image for each post on this site.</p>
<h2>The Custom Field Panel</h2>
<p>As shown in this post&#8217;s header image, the custom fields panel has two form fields, one called &#8220;Name,&#8221; and the other &#8220;Value.&#8221; Name is how you&#8217;ll reference this custom field when accessing it in the code and the value is, well, whatever you want it to be. As an example, the post header images on this site use a name of &#8220;image&#8221; and a value of &#8220;post-image-name.jpg&#8221; (this image file name).</p>
<p>Depending on whether or not you&#8217;ve ever used custom fields before, your custom field panel may not look exactly like the one above. Instead you may see something more like this:</p>
<div id="attachment_1246" class="wp-caption aligncenter" style="width: 563px"><a href="http://www.rlmseo.com/wp-content/uploads/2009/12/wordpress-custom-fields-screenshot.jpg" rel="wp-prettyPhoto[581]"><img class="size-full wp-image-1246" title="Enter a new custom field in WordPress" src="http://www.rlmseo.com/wp-content/uploads/2009/12/wordpress-custom-fields-screenshot.jpg" alt="Enter a new custom field in WordPress" width="553" height="174" /></a><p class="wp-caption-text">Enter a new custom field in WordPress</p></div>
<p>This allows you to enter a new custom field if you haven&#8217;t entered any before or if you already have existing custom fields and click the &#8220;Enter New&#8221; button as seen in this post&#8217;s header image.</p>
<h2>Adding a New Custom Field</h2>
<p>So now that we&#8217;re familiar with the custom field panel, let&#8217;s use it.</p>
<p>Since I&#8217;m currently working on a WordPress Real Estate blog, we&#8217;ll create a custom field to display the property address at the bottom of the page, but you can use this technique to add just about anything to a post.</p>
<h2>Creating a Property Address Custom Field</h2>
<p>So the first thing we want to do is type the name by which we want to refer to this custom field in the &#8220;name&#8221; field of the custom fields panel. In this case I&#8217;ll use &#8220;property_address&#8221;. You can actually use anything you want and it can even have spaces in it, although I prefer to avoid spaces, replacing them with underscores, just in case that changes in future WordPress releases.</p>
<p>Next we want to add the property address to the &#8220;value&#8221; field. Keep in mind that if you want to add any formatting inside the custom field (like line breaks for instance), you&#8217;ll have to add the HTML code directly to the field itself here because we&#8217;ll be pulling the data out of the field directly and WordPress won&#8217;t automatically add formatting like it will when writing the actual content for a post. An example of that can be seen toward the end of this post.</p>
<p>Here&#8217;s what it looks like before we save it:</p>
<div id="attachment_1247" class="wp-caption aligncenter" style="width: 563px"><a href="http://www.rlmseo.com/wp-content/uploads/2009/12/adding-a-new-custom-field-value-in-wordpress.jpg" rel="wp-prettyPhoto[581]"><img class="size-full wp-image-1247" title="Adding a Custom Field Value in WordPress" src="http://www.rlmseo.com/wp-content/uploads/2009/12/adding-a-new-custom-field-value-in-wordpress.jpg" alt="Adding a Custom Field Value in WordPress" width="553" height="174" /></a><p class="wp-caption-text">Our new Custom Property Address Field</p></div>
<p>Now that we&#8217;ve entered the name and value, click the &#8220;Add Custom Field&#8221; button to apply our changes.</p>
<p>Now save/publish the post.</p>
<h2>Editing Our Post Template to show the Property Address</h2>
<p>Now that we&#8217;ve created our new property address custom field, we have to tell WordPress where we want that information displayed.</p>
<p>For this example, I&#8217;m going to add code to the <span class="code">single.php</span> theme template to first check if that property address was entered for the post being shown, and then to display that address if it exists.</p>
<p>So, open up single.php and look for the code that displays the post content, which is something like this:</p>
<pre>&lt;?php the_content(); ?&gt;</pre>
<p>Your <span class="code">the_content()</span> function may contain something inside the parentheses and that&#8217;s fine&#8230;we&#8217;re just looking for that line.</p>
<p>Now immediately after that line we want to add the following code:</p>
<pre>&lt;?php
$property_address = get_post_meta($post-&gt;ID, 'property_address', true);
if($property_address != '') echo "&lt;strong&gt;Property Address: &lt;/strong&gt;" . $property_address;
?&gt;</pre>
<h2>get_post_meta()</h2>
<p>The first line of that code pulls the custom field with the name &#8220;property_address&#8221; and assigns it to the <span class="code">$propert_address</span> variable using WordPress&#8217; built-in <span class="code">get_post_meta()</span> function.</p>
<p>That function takes up to three parameters: The first is the post ID number. Since we&#8217;re adding this code inside the loop, we can use <span class="code">$post-&gt;ID</span> to get the post ID number. If you&#8217;re using this code outside of the loop, the <span class="code">$post</span> object won&#8217;t be set so you&#8217;ll have to enter the ID number of the post you want to use here. The second parameter is the name of the custom field we want, in this case, &#8220;property_address&#8221;. And the third parameter tells WordPress to return the value of that custom field as a string, rather than inside an array.</p>
<p>The second line uses an if-statement to check if the <span class="code">$property_address</span> variable contains anything besides an empty string. If there&#8217;s something there, we then echo &#8220;Property Address:&#8221; in bold, followed by the contents of the <span class="code">$property_address</span> variable containing our property address.</p>
<h2>The Final Result</h2>
<p>The final result, using my free WordPress theme, This Just In!, looks like this:</p>
<div id="attachment_1248" class="wp-caption aligncenter" style="width: 563px"><a href="http://www.rlmseo.com/wp-content/uploads/2009/12/wordpress-custom-field-output.jpg" rel="wp-prettyPhoto[581]"><img class="size-full wp-image-1248" title="WordPress Custom Field Output" src="http://www.rlmseo.com/wp-content/uploads/2009/12/wordpress-custom-field-output.jpg" alt="WordPress Custom Field Output" width="553" height="174" /></a><p class="wp-caption-text">Our custom field output beneath a post</p></div>
<p>That&#8217;s all there is to it.</p>
<h2>Some things to remember about custom fields</h2>
<p>You can use custom fields in this way for just about anything. You should remember that the custom field is pulled as plain text exactly as you entered it, so if we wanted to display the address on two lines, like it would be shown on a letter for example, we&#8217;d have to add <span class="code">&lt;br /&gt;</span> tags into the custom field when creating it. You&#8217;ll probably want to output the custom field inside of a table in this case so that the second line of the address wraps below the first line properly. The code to display that would look something like this:</p>
<pre>&lt;?php
$property_address = get_post_meta($post-&gt;ID, 'property_address', true);
if($property_address != '') {
echo "&lt;table&gt;";
echo "&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Property Address: &lt;/strong&gt;&lt;/td&gt;&lt;td&gt;" . $property_address . "&lt;/td&gt;&lt;/tr&gt;";
echo "&lt;/table&gt;";
}
?&gt;</pre>
<p>In this case, our custom field panel would look something like this:</p>
<div id="attachment_587" class="wp-caption aligncenter" style="width: 563px"><img class="size-full wp-image-587" title="WordPress Custom Field with HTML Formatting" src="http://www.rlmseo.com/blog/wp-content/uploads/2009/12/wordpress-custom-field-html-formatting.png" alt="HTML formatting in our custom field" width="553" height="174" /><p class="wp-caption-text">HTML formatting in our custom field</p></div>
<p>And the final result would be something like this:</p>
<div id="attachment_588" class="wp-caption aligncenter" style="width: 563px"><img class="size-full wp-image-588" title="WordPress Custom Field with HTML Formatting Result" src="http://www.rlmseo.com/blog/wp-content/uploads/2009/12/wordpress-custom-field-html-formatting-result.png" alt="The final result with HTML formatting" width="553" height="225" /><p class="wp-caption-text">The final result with HTML formatting</p></div>
<h2>More about custom fields</h2>
<p>Smashing Magazine has a <a title="Custom fields at Smashing Magazine" href="http://www.smashingmagazine.com/2009/05/13/10-custom-fields-hacks-for-wordpress/" target="_blank">great post</a> about other uses for custom fields if you&#8217;re interested in learning more.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rlmseo.com/blog/wordpress-custom-fields/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

