Wordpress Custom Fields for Better Blog Navigation
I use Wordpress for a full Content Management System (CMS), not just a blog. I use WP to sell digital products, park domains, and post news. I have to say that Wordpress can and SHOULD be your starting point when starting any new web project. All that aside, there are many things that wordpress can do, but they are not well know. One of those is custom fields. To understand how custom tags work, I need to demonstrate the Key / Value pair and how to find custom fields for blog posts.
Custom fields are added to any post or page and is located below the WP editor along with the other hardly used post additions. They are made of a Key/Value pair. In this examples, I will be grouping a few of my posts into projects. I consider a project 2 or more posts that should be viewed together. Usually, you will see these as part 1, part 2, etc… Since my website will have many projects I will b using 1 custom field key, with many custom field values.
Example:
| Key | Value |
|---|---|
| project | domain_parking |
| project | domain_parking |
| project | themes |
| project | themes |
As you can see I can have many projects… in this case I have 2, one for my posts on domain parking, and one on themes. Now I need to get the key/value and do something with it. This code snippet will do that.
$project = get_post_meta($post->ID, 'project', true) ;
I put this in the loop on my single.php file. I pass the post id, the custom field key (project), and I pass true to only get the string variable. passing false will return an array. it will return nothing if no project was defined.
Right below that single line of code, you can add this to find the other posts that are part of the same project.
$project = get_post_meta($post->ID, 'project', true) ;
$posts = get_posts();
echo "<ul>";
foreach ($posts as $post)
{
if(get_post_meta($post->ID, 'roadmap', true) == $roadmap)
{
echo "<li><a>guid.">".$post->post_title."</a></li>";
}
}
echo "</ul>";
This code, goes through all posts and checks to see if the post belongs to the same project as the main post. If it does belong to the same project it outputs the link. Pretty simple and it has a lot more uses then just better navigation.
I also use it with my Ebay affiliate plugin. I ue a key of “ebay_items” with a value of anything that I want to find on ebay. Then for each post, if ebay_items is set, I will query Ebay and return products based on the ebay_item value.
Twitter Tools Wordpress plugin, also uses custom fields to ping Twitter.com with your posts.
| US $19.98 (0 Bid) End Date: Wednesday Jan-07-2009 6:41:45 PST Bid now | Add to watch list |
| US $0.99 (0 Bid) End Date: Wednesday Jan-07-2009 9:54:01 PST Bid now | Add to watch list |
