Different Uses for Custom Fields in WordPress
You can use Custom Fields to define all sorts of data on your WordPress posts and pages; you’re limited only by your imagination when it comes to what kind of data you want to include.
Obviously, the possible uses for Custom Fields are endless, but here are a few ideas that you may want to try on your own site. At the very least, you can implement some of these ideas to get yourself into the flow of using Custom Fields, and they may spark your imagination on what types of data you want to include on your site:
- Music: Display the music you’re currently listening to. Use the same method below, except create a Custom Field named music. Use the same code template, but define the key as
$key="music";and alter the wording from My Current Mood is: to I am Currently Listening to:.
- Books: Display what you’re currently reading by creating a Custom Field named
book, defining the key in the code as
$key="book";, and then altering the wording from My Current Mood is: to I Am Currently Reading:.
- Weather: Let your readers know what the weather is like in your little corner of the world by adding your current weather conditions to your published blog posts. Create a Custom Field named
weather, and use the same code for the template. Just define the key as
$key="weather";and alter the wording from My Current Mood is: to Current Weather Conditions:.
If you want to get really fancy with your Custom Fields, you can also define an icon for the different metadata displays. Using the mood Custom Field, for example, you can add little emoticons (smiley-face icons that portray mood) after your mood statement to give a visual cue of your mood, as well as a textual one. Follow these steps to add an emoticon to the
mood Custom Field:
- Visit the Posts screen on the Dashboard by clicking the Posts link on the left navigation menu.
- Click the title of the post that you want to edit.
- Add a new Custom Field by choosing Enter New link from the drop-down list and entering mood-icon in the Name text box.
- Click the Add Media button above the Post text box to open the Add Media window.The Add Media window opens.
- Click the Select Files button, and upload an image from your computer.
- From the Link URL text box, copy the file URL of the image you uploaded.
- Click the X in the top-right corner to close the Add Media window.
- Paste the Link URL in the Value text box for the mood-icon Name.
- Click the Add Custom Field button.The Name and Key values are saved.
- Click the Update button in the Publish module.The changes in your post are saved and updated on your site.
- Update the function code in your template file to include the new mood icon.
- Follow these steps to add that code:
- Click the Editor link on the Appearance menu of your Dashboard.
- Click the
content-single.phptemplate displays in the text box on the left side of the page.
- Locate the code you added for the
- Before the closing
</strong>HTML tag, add the following line of code:
<img src="<?php $key="mood-icon"; echo get_post_meta($post->ID, $key, true); ?>"/>The
<img src="that precedes the Custom Field code is the HTML tag used to display an image on your site. The
" />code that appears after the Custom Field code is part of the HTML tag, and it closes the
<img src="HTML tag. Change the
$keyto indicate that you’re calling the
- Click the Update File button to save your changes.
- Visit the post on your site to view your new mood icon.
The entire snippet of code you added in these steps should look like this when put together (be sure to double-check your work!):
<?php if ( get_post_meta($post->ID, 'mood', true) ) : ?>
<p><strong>My Current Mood is:</strong> <em><?php $key="mood"; echo get_post_meta($post->ID, $key, true); ?></em> <img src="<?php $key="mood-icon"; echo get_post_meta($post->ID, $key, true); ?>"/></strong></p>
<?php endif; ?>