For many WordPress users the custom fields box is an unused section on the Write Post/Page page. What exactly are custom fields? Custom fields allow you to assign additional information to that particular post (or page, but from here on out I’ll just call it a post), and only that post ie. the information you add will only be available to that page.
For example, maybe you want to include a unique comment for every post you write, but display this in the sidebar, or have it go in your header.
Set the Custom Field
First off, you open up the Write Post page and write your post as normal. Then scroll down to the Custom Fields section and open it up. Now we need to select your key. Check the drop down list to see which keys already exist. Let’s call this key ‘quote’. If it’s not in the select list then type it in the text input box to the right of the select list (once you’ve typed it in once it will appear in the select list as an available key for all posts and pages on your site). Then in the textarea you can type the value for this key which would be a quote from your post, for example for this post I could use
For many WordPress users the custom fields box is an unused section
Once you’ve added your quote click the Add Custom Field button to save it. Then Save or Publish the post. At the moment the quote won’t be seen, so now we need to edit the template to show it.
Display a Custom Field
We’re adding this quote to the header, however you could add it in any template file. For this, we would open up the header.php theme file and decide where to add the quote. To retrieve the value of the quote we simply need to use
<?php echo get_post_meta($post->ID, 'quote', TRUE); ?>
Here the template tag we’re using is get_post_meta() which can take 3 parameters. The first is the ID of the post, the second is the name of our key and the third controls whether we’re looking for a single value for this key or multiple values for this key. In this instance we’ll only have one quote per page so this is set to TRUE (meaning single, FALSE or omitted would make it search for all values of the key ‘quote’ assigned to the post).
This PHP code can of course be put in between blockquote tags, or any HTML tags.
Custom Field Uses
A number of popular plugins use custom fields to do their work, however you don’t realise this as the plugin authors have essentially duplicated the custom field input boxes and given them names, for example the All in One SEO pack uses custom fields for the page title, meta description and meta keywords. Of course this offers a number of other options too, and as always, plugins take the hard work out of editing your theme, but you could just use the custom fields section to add a meta description and use the PHP code above to print the description for that post or Page into the meta description tag.
Other examples of use could be
- To select a specific image for your header
- To add in just a meta description for your post without the need for a plugin
- To add in the language type of the post (if you wrote in more than one language) and have this output into the language attributes of the HTML tag
- To create a unique piece of information for your sidebar
- To add in details for a sponsor for that particular post
There are many options for this under-used feature 🙂