Breaking

Tuesday, January 5, 2016

Display Your Mood Or The Music You’re Listening To


The problem. About five or six years ago, I was blogging on a platform called LiveJournal. Of course it wasn’t great as WordPress, but it had nice features that WordPress doesn’t have. For example, it allowed users to display their current mood and the music they were listening to while blogging. 

Even though I wouldn’t use this feature on my blog, I figure many bloggers would be interested in knowing how to do this in WordPress.  
The solution. Open your single.php file (or modify your index.php file), and paste the following code anywhere within the loop:
view plaincopy to clipboardprint?

  1. $customField = get_post_custom_values("mood");
  2. if (isset($customField[0])) {
  3. echo "Mood: ".$customField[0];
  4. }
Save the file. Now, when you write a new post, just create a custom field called mood, and type in your current mood as the value.
Code explanation. This is a very basic use of custom fields, not all that different from the well-known hack for displaying thumbnails beside your posts’ excerpts on the home page. It looks for a custom field called mood. If the field is found, its value is displayed.

No comments:

Post a Comment