Today I’m going to show you how to create custom fields in WordPress with the Advanced Custom Fields plugin and display them using the PHP function ‘the_field’ in the posts, pages or in any custom post type you create.
With this plugin we can create custom fields for text, email, password, Google maps, etc. that will be saved in our database and we can display on our website so that users can view that data.
CREATING CUSTOM FIELDS IN WORDPRESS
Once installed and activated the plugin we will go to the custom fields tab and there we can create all the fields we need easily. We can also choose in which template these changes will be applied such as page.php or single.php.
Let’s imagine that we want to make a book review website and we want to modify the entries to have several fields by default such as author, argument, file download and ratings. We will create the fields and select which template it will affect, in this case the entries. Once the changes are saved if we go to create any entry we will see that on our desktop we have all the fields by default that we can fill in every time we make a new book review.
SHOW CUSTOM FIELDS IN WORDPRESS
Once the fields are filled in and the new entry is published we will see that the data of these fields have been saved in our database but if we go to see the entry we will not see any result, so, we are going to add a function in our entry template with which we can show the fields for the user.
For this we will use the PHP function the_field:
<?phpthe_field('text_field');?>
Where we will replace the text ‘text field’ with the name of the field we have created.
Next we will go to the template where our entries are displayed and paste the function ‘the_field’ , just before or after the function ‘the content’. We can use HTML with the function to add a name and make everything much better visually. I remind you that the best option is always to do it through FTP and to the child theme. It is not a good idea to do it directly in the WordPress editor.
Once the changes are saved if we go to see the book review entry that we created before we will see that you can already see the custom fields. Once the changes are saved if we go to see the book review entry that we created before we will see that you can already see the custom fields. And that’s it, as you can see it is very easy to create custom fields and display them in the front-end.
If you have any questions or suggestions or just want to tell me something, you can send me an e-mail through the contact form or leave a comment in this post and I will answer you as soon as possible.
Leave a Reply