Kriesi.at – new media design

How to use WordPress Custom Fields

WordPress gives an author the ability to add extra data to each written post and page. This data is called meta-data and is stored in custom fields.

These fields are really flexible in use and make it possible for developers and theme-authors to create stunning sites, far beyond from normal blog design.

In case you have never noticed it: to access these fields just head over to the write post/page site down to the Advanced Options Tabs. There you will find  the Custom Fields Tab which looks something like this:

Custom fields consist of two parts: A key and a value.

There are different options to display those fields, the one thing they all got in common: you have to call them inside the loop.

<?php the_meta(); ?>

This is the easiest way to display the data. The template tag automatically puts the entire meta-data into a CSS style called post-meta. The key is in a span called post-meta-key.

All of this is showcased in an unordered list:

<ul class='post-meta'>
<li><span class='post-meta-key'>mood:</span> happy</li>
<li><span class='post-meta-key'>Weather:</span> fine</li>
</ul>

You might want to use these custom informations in a more sophisticated way then displaying unordered lists with your mood and the current weather. Especially if you want to display a post in a way that doesn't remind the visitor of a typical blog post, custom fields come in very handy.

Just take a look at my porfolio page, or if you want to see a whole site based around custom fields, head over to BestWebGallery. Each post is stuffed with a lot of meta data which is used to display the posts in an unique way.

To display the data in a superior way we use the function

 get_post_meta($post_id, $key, $single);

The parameters we need to pass are explained fast:

  • $post_id is the ID of the post which stores the meta data. Most of the time it is the current post and we use: $post->ID
  • $key is a string containing the name of the meta value you want.
  • $single can either be true or false. If set to true then the function will return a single result, as a string. If false, or not set, then the function returns an array of the custom fields.This is needed if you have different $keys with the same name. We will set this to true for our example.

So lets say we have a custom field named "image" which contains the URI to the image:

we could easily display this image the following way:

<?php $image = get_post_meta($post->ID, 'Image', true); ?>

<img src="<?php echo $image; ?>" alt="" />

There are many ways to use custom fields, another one would be to add  a different class to some of your posts:

<?php $additional_class = get_post_meta($post->ID, 'class', true); ?>

<div class="my_post <?php echo $additional_class;?> ">
the_content();
</div>
?>

The possibilitys are nearly endless, so let your creative juices flow and enhance your site with custom fields ;)

Responses to “How to use WordPress Custom Fields”

Trackbacks

  1. pligg.com
  2. Кэширование в WordPress (1/3)
  3. Wordpress Custom Fields | How To Blog
  4. 10 Custom Fields Hacks For WordPress | How-To | Smashing Magazine
  5. Custom Fields Hacks For WordPress « N3T.ir - Web Resources, Web Design News & Tips, Open Source
  6. Custom Fields Hacks For WordPress | jeremiahnellis.com/design_life
  7. Custom Fields Hacks For WordPress « Best Design Content
  8. Custom Fields Hacks For WordPress | B Studio
  9. Grumpy Git . org » Blog Archive » Custom Fields Hacks For WordPress
  10. Custom Fields Hacks For WordPress | The Scripts Zone
  11. Weekly roundup 7-12-2009 « Resonant Chaos
  12. Sumit-Online.de » Blog » Wie du Custom Fields benutzen kannst
  13. Custom Fields Hacks For WordPress | Benzing Technologies | Web Design, Web Hosting, Online Marketing | 866.980.2369
  14. BSoA 2
  15. Places you can learn how to set up cool stuff using Wordpress custom fields | Design strike
  16. Custom Fields Hacks For WordPress | 9Tricks.Com - Tips - Tricks - Tutorials
  17. Blogging in a new way « Ankit’s Weblog
  18. Advanced Power Tips For WordPress Template Developers - Smashing Magazine
  19. Advanced Power Tips For WordPress Template Developers « Tech7.Net
  20. Advanced Power Tips For WordPress Template Developers – Upon A Star
  21. Custom Fields « Netxm’s Blog
  22. 11 Most Common Wordpress Posts | New 2 Wp

Leave a Reply