How to echo a Custom Field in WordPress from a PHP page/post template

by Sam Davis
If you’re an experienced user of WordPress, you may be used to utilising the Custom Fields section that is available within Posts and Pages. The Custom Fields are a standard feature of WordPress which can enable extra information to be attached a post. If you cannot see these fields within your Admin, you can click the Screen Options tab at the top of the page to enable the Custom Fields (often referred to as “meta-data”).
You may actually find that a number of your existing plugins on your WordPress site may already be making use of these Custom Fields without you realising it, as they may be storing some settings that vary from post to post. As an example, when I write a blog post on my site, I have a checkbox which determines whether or not I’d like it to ping users who have agreed for receive notifications whenever when I post the blog. Whilst it appears on my Admin page as a nice little checkbox, when I toggle it, what it ACTUALLY does is to alter the value in the relevant Custom Field accordingly.
It must realised that the use of Custom Fields is not limited to plugins though. The beauty of meta-data is that users can configure their own variables for a post, entering a value accordingly – which can be called using a little chunk of PHP code.
Examples could include the following scenarios:
- Writing a travel blog? You may want to enter the geographical location of your blog post by adding the city, country or co-ordinates.
- Is your blog an online diary? If so, you may want to type your “mood”.
- Real Estate website? You may want to place specific details about a property in the Custom Field such as bedrooms, measurements or EPC ratings.
You could style your values within the template to make it appear as a graph, a link, or a Google Map for populating the longitude and latitude variables of the provided code dynamically
Although you obviously can enter all this information in the body of the post, by entering it into a Custom Field, you give yourself the opportunity of being a bit more creative with displaying this meta-data. For instance, you could style your values within the template to make it appear as a graph, a link, or a Google Map for populating the longitude and latitude variables of the provided code dynamically… the possibilities are endless.

You may particularly find that going in the direction of using Custom Fields can be useful when there are multiple editors of a website where you do not want one of them to inadvertently “break” the format of your posts. This means the body of the post can be left to being text only – but cleverly, you can subsequently call any Custom Field values by inserting a block of PHP into your blog template. Incidentally, the code to use in your template to call the value of a variable you have created, is:
<?php echo get_post_meta(get_the_ID(), 'Location', TRUE); ?>
The above line, when placed in your blog template, will call a variable called “Location” and display the appropriate value accordingly.
In most cases, your blog template will be called single.php and you’ll be able to find it in your theme folder. You can experiment by pasting the above code below the_content() – which is the part that displays the body of your blog post.
GET IN TOUCH IF YOU NEED ANY HELP
Sam Davis
I am fanatical about providing useful web experiences, via bespoke websites which engage, inform and entertain. I have been making websites since a young age, and am a keen supporter of AFC Bournemouth.




