Discover in this tutorial how to display custom fields in WordPress using a shortcode.
Although I’m more in favor of creating blocks to display custom fields (especially using Advanced Custom Fields) there are still situations where it’s more practical to get them and display them using a shortcode.
So in this tutorial you are going to learn how you can achieve this.
Steps to display a custom field via shortcode in WordPress
1. Create the shortcode
Add the following code at the end of functions.php or in your functionality plugin:
With this code you create a shortcode that allows you to display a custom field, associated with the post, using the shortcode [custom_field data=”x”].
Here you can replace “x” with the custom field you want to display, as we are going to see in the next step.
2. Insert the shortcode wherever you want
The advantage of shortcodes is that you can insert them anywhere on the website.
You only have to use the shortcode by defining the “data” attribute where you want it to be displayed.
For example, imagine you have created a workshop CPT with a custom field called “duration”. This would be the shortcode you would use to display the value:
Now you know how to get and display any custom field using a shortcode in WordPress.
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉