The ability to tailor and customize to the author’s whims and needs is one of the most powerful aspects of the WordPress experience. Metadata accompanying a post is one such example of WordPress’s customizability, allowing you to convey important and relevant information to your readers without cluttering or detracting from the main content of the post.
With custom post meta, you can express supporting information like your mood, current activities, location, and more right alongside your content in a way that doesn’t detract from the content itself. Setting up your WordPress installation to display custom post meta is just a matter of a few easy steps. We’ll detail them below.
What is Custom Post Meta?
While there are a wide variety of possible uses for custom post metadata, this tutorial will focus on it as any supporting information that you would like to add into your post without it being a part of the main content of the post itself. You will also hear it referred to as custom fields, as that is the header you are going to be working under in your WordPress post editor.
Navigate to the post editor screen of your WordPress admin panel. You may add a new post or edit an existing post. Scrolling down the page you should see a box labeled Custom Fields. This is where you will enter your metadata. If you don’t see this on your screen, you need to click Screen Options at the top right of your screen and then enable Custom Fields. See the screenshot below for reference.
Post meta works as a system called key/value pairs. The key is a static designation that you assign as the name of your custom field, and how WordPress recognizes this instance of your metadata. Your readers will not see this designation: using the following method only the value will be displayed. The key is entered into the Custom Fields box under “Name”. In this example, we’ll assign the Name “test”.
The value is the data that WordPress associates with the key, and is the information your readers will see alongside the main content of your post. In this example, I want to display that I am currently reading A Song of Ice and Fire, so I will enter click “Enter new”, name my custom field “test” (this does not display for your users, it’s just the name that WordPress will associate with this bit of metadata), and in the value field type “Now Reading: A Song of Ice and Fire”. My Custom Fields box now looks like this:
If you publish your post at this point, your metadata is present in the post editor but will not be displayed with the published post. Also, where this metadata displays alongside the main content in your published post can happen in a variety of different ways depending on your preference. How and where to display your metadata is covered in the next section.
How to Display Custom Post Meta
Before we begin, consider the following: to display your custom post meta, we are going to be editing your theme’s PHP. That means any future updates are going to overwrite your changes and you will have to re-edit with each theme update. It is highly advisable that you work around this by making a child theme for your current theme, and that you make the following code edits (and all theme edits in general) to the child theme’s PHP. This ensures your changes are as permanent as you desire them to be.
The following instructions are for the twentyfifteen theme, but are generally consistent across themes. In twentyfifteen, you are looking for your content.php file. It can be found in the path wp-content>themes>twentyfifteen.
**The location of the file you need to edit will always be in your themes folder, but due to the abundance of different themes and their backend design, the name of the file you need to edit may vary. For example, in Solon it is called content-single.php.
The code that needs to be pasted into your content file is the get_post_meta function call. This function will return the value you associated with the key in the post editor, and display it in a location of your choice within the post. You can paste the following line of code directly into your content file:
Note that you need to replace the word ‘key’ with what you have assigned as the key/Name of your metadata in the post editor. In this example, we named our metadata test, so our line will read as so:
Where you paste it will determine where your metadata displays in your published post. Below is a screenshot of twentyfifteen’s content.php edited so that our metadata will display after the post’s title but before the main content. You can move it around inside the loop to have it display in other locations if you wish, such as before the post title or after the main content.
Be sure to save your edited content.php, publish your post, and at this point your post should display your custom meta as so:
With custom post meta, you can express supporting info like your mood, activities, location, and more. Learn how!Click To Tweet
You now know how to edit and display custom post meta. As mentioned earlier, there are many very broad applications of custom post meta, but in this context you have a great way to give your readers extra snippets of relevant information without taking away from the main focus of your posts. Check back with WP Ninjas for more applications of custom post meta and other tips, tricks, and tutorials in the future!