The Montoya Herald — ChristianMontoya.com
I haven't done any how-to posts in a while, but it's time to put the counter back to zero. You might have noticed that I launched a blog about Facebook Application Reviews. It's all about, um, reviews of Facebook applications, where each post contains a review for an application made for Facebook, and each review contains a rating. If there's one unspoken convention across the Internet for reviews, it's stars. Nice, yellow stars, one for each point in the rating. So when I started Facebook App Reviews, I knew I needed stars, and that's where custom fields came in.
This is where the magic of custom fields happens:
If you want to make a new field, you put the name in the box in the middle and the value in the box at the right. Once you have done this, the field will be available from then on, and you can set a value for it on each subsequent post. The next step is just getting that value back in your template. Now, I know that the name of my field is "rating," and every post in the category "Reviews" is going to have it, and it's always going to be a value between 1 and 5. All I have to do is pull that value out in each post. Here's the code I use:
$rating = get_post_meta($id, 'rating', true);
Seriously, that's it. The post id ($id) came from the current post in The Loop, which you can get with get_the_id();. The second argument is the name of the field, and the last argument is "true" if you just want the first result (perfect if there will never be more than one entry per post) and "false" if you want to get an array. It's very simple and I kinda feel lame writing a how-to for something so simple… but I guess if you didn't know about this already, you might have found this useful. Just think about the things you can do… any kind of custom information you want to apply to your posts can happen right here… I mean, I can't think of any specific examples at the moment, but I'm sure you can come up with something. Put it in the comments here when you do.
Ha, that's easy. I was planning to use custom fields with a plugin, but this method works way better.
Thanks!
I have had a go at using custom fields to take the details of an event so that the blog can then output it as a micorformatted hCalendar event. It sort of worked, but it was a hassle entering all the information through individual custom fields and I couldn't think of a better way to do it. Also, when it came to displaying the events in a list separate to their home posts things started to drag too. I'm sure something could be done with this idea, but I don't have the time to continue investigating at the moment.
Phil: Sounds like what are working with would be better handled by a plugin. They aren't too hard to make, and you could design a better interface than the generic one that Wordpress has for custom fields.
You're right, but at the outset I liked the simplicity of custom fields… I guess they are more suited to simple applications like your star ratings. Maybe I will finish/rewrite the whole thing someday, whether I use custom fields or not I'd like to add something to both the WordPress community and Microformats too.
Custom fields are one of the most useful tools for extending Wordpress. I used to use the custom fields gui plugin along side so you could even have a drop down of 1-5 below your posting box on your write post page to speed up your posting… maybe you would find that useful. Slightly quicker then using the custom fields pane, adding a gui to custom fields is especially useful for client work.