WordPress Custom Fields Data Display
Adding Custom Fields to your WordPress template files facilitates proper data display on your WordPress blog. To get the data to display properly, you must open the template files and dig into the code! The easiest way to do this involves using the get_post_meta(); template tag function, which looks like this:
<?php $key=NAME; echo get_post_meta($post->ID, $key, true); ?>
Here’s how that function breaks down:
Every template tag or function needs to first start PHP with <?php.
This defines the name of the key that you want to appear. You define the Name when you add the Custom Field to your post.
This grabs the Custom Field data and displays it on your site.
A parameter of the get_post_meta function that dynamically defines the specific ID of the post being displayed so that WordPress knows which metadata to display.
A parameter of the get_post_meta function that gets the value of the Custom Field based on the name, as defined in the $key=NAME; setting earlier in the code string.
A parameter of the get_post_meta function that tells WordPress to return a single result, rather than multiple results.
By default, this parameter is set to true; typically, don’t change it unless you’re using multiple definitions in the Value setting of your Custom Field.
This ends the PHP function.
For example, based on this code, to make a mood in the Custom Field you define the key name as mood (replace the NAME in the preceding code with the word mood); it looks like this:
<?php $key=mood; echo get_post_meta($post->ID, $key, true); ?>
The part of the functions that says $key=mood; tells WordPress to return the Value for the Custom Field with the Name field of mood.