Subscribe to RSS Feed

TechFold is technology discussion, commentary, reviews, and opinions from well outside the valley. There's no koolaid to drink here, and TechFold is not in SL, or on Twitter.

WordPress Expert - Part Two: Doing cool stuff with Custom Fields

As part of the ridiculous effort to transition UpcomingDiscs.com from custom CMS to WordPress, one thing I’ve had to do is figure out to replicated a bunch of custom coded functionality - things like DVD Review Ratings for instance - UpcomingDiscs rates discs across a number of criteria (audio and video quality, film quality, etc.) - how to get those to show up in WordPress?

Here’s how the ratings look on UpcomingDiscs right now:

Option one is to just hand code the rating graphics into every review posted on WordPress. But that seems like a colossal PITA, and will add time and complexity to the process of posting a review. Given that the UpcomingDiscs reviewers are not a specifically technical bunch, this isn’t really an option.

So - I turned to the WordPress custom fields feature. If you’re a WordPress user, you may have noticed Custom Fields at the bottom of the “Write” page - its an expandable blue header bar, that expands to something like this:

Custom Fields lets you enter any number of bits of info that you want to - in nicely structured key & value pairs. The “key” is the name of the bit of data that you want to enter - for example, “Audio Rating,” which I’d enter as “audio_rating.” The value is the audio rating that I want associated with the review I’m writing, i.e.: “4.0″. Once you’ve entered a key once, WordPress makes it available in the drop down that you see in the right-hand side of the custom fields area.

A fully filled out set of custom fields for an UpcomingDiscs review looks something like this:

Ok - that’s the first step - getting your custom data into a WordPress post in a nicely organized fashion. Now, you need to do something with it, so that its presented to your readers in your posts. This part gets a little dicey: you need to do some PHP customization work in your blog’s template — fortunately, WordPress makes it easy with a well structured way of accessing the custom field data that you’ve stored.

For the purposes of UpcomingDiscs, I only want my ratings to show when someone has opened a blog post on its own page; i.e.: I don’t want them cluttering up the front page of the site. To that end, I need to edit only the “single.php” template page, found under “/wp-content/themes/yourtemplate.” Read through your single.php file, and you’ll find it to be pretty self-explanatory - WordPress themes generally use nice, clean HTML and css formatting.

So - the first thing you want to do is retrieve the data you’ve stored as Custom Fields, and get it all into usable PHP variables. WordPress has an easy to use hook for just this purpose; here’s my retrieval code:

$overall = get_post_meta($post->ID, 'overall', 'true');
$film = get_post_meta($post->ID, 'film', 'true');
$video = get_post_meta($post->ID, 'video', 'true');
$audio = get_post_meta($post->ID, 'audio', 'true');
$extras = get_post_meta($post->ID, 'extras', 'true');

  1. “get_post_meta” is the WordPress function for retrieving Custom Field data.
  2. “$post->ID” is just the WordPress ID number for the post you want to retrieve data for; in this case, the page has only one post, so we just feed in that ID.
  3. The next argument is the name of the bit of data you want to retrieve - this is the name that you entered as the “key” in the Custom Fields area when you wrote the post.
  4. The “true” argument at the end tells the function to return found data as a string; leaving it unset returns data as an array.
  5. If the Custom Field you’re looking for in a given function call doesn’t exist or is blank for the post in question, an empty string is returned.

Now - you can code whatever you want using the variables that you’ve retrieved. I pass the values to an external function that returns an appropriate image tag to display the ratings like this:

The code to do so is pretty straightforward…

if ($video != "") { echo "<div class=\"item_rating\">Video " . get_rating_graphic($video) . "</div>"; }

And that’s it in a nutshell! Stay tuned for episode three….

If you enjoyed this post, make sure you subscribe to my RSS feed!

Related Posts

UpcomingDiscs.com: WordPress conversion complete
I’m Back and Busy
Part One - Dissecting the WordPress Import/Export Format: Categories
How Wordpress.com Stays Spam Free
WordPress Tip - Schedule your posts

7 Responses to “WordPress Expert - Part Two: Doing cool stuff with Custom Fields”

  1. SigT |

    Cómo generar valoraciones con los “Custom fields”…

    En TechFold están actualizando una página de cine de su CMS a WordPress y como uno de los pasos de la transición quieren seguir ofreciendo las valoraciones por lo que las hacen usando los campos personalizados (entrada en inglés) lo cual supone un …

  2. Rod |

    Hey SigT, glad you found it useful!

  3. Cómo añadir más información a nuestros posts en WordPress » blogpocket 7.0 |

    […] Otra aplicación práctica: valoraciones de películas | | Añadir a del.icio.ustags: Wordpress […]

  4. John |

    Wow, neat use of custom fields! Thanks for the tip.

  5. Jazmin |

    Hi there,

    Thanks for the great post. It’s extremely helpful.

    I’m hoping you can help me solve a custom field problem that I’ve been researching all week (but obviously haven’t found the answer to). Do you know how to incorporate custom fields into a feed?

    Specifically, I use custom fields to display images for my posts as well as to display thumbnails for the post excerpts in my archives. I’d like the thumbnails to show up in the excerpted posts in my feed. Any advice on how to do this or where else I can look for an answer?

    Thanks so much for your help!

  6. Rod |

    Hey Jazmin: I’m sorry to say that I’ve never looked at whatever it is in wordpress that produces the RSS. I’m on the road (travelling in asia) at the moment, so I’m not really going to have a chance to do so for several months either - so anyway, I apologize but you’re on your own for this one! Good luck, and please comment back with your solution if you have a moment to do so.

    Regards,
    –Rod

  7. Jazmin |

    Hey Rod, thanks for the response! And, no worries, I’ll keep digging and I’ll be sure to report back if I ever do find a good solution. But for now, it looks like I’ll be doing it the old fashioned way and just hard coding the thumbnail into the “Optional Excerpt” box ;)

    Have a safe trip and happy travels!

Leave a Reply

Close
E-mail It