In this tutorial we will see how to create and use custom fields under WordPress
A custom field under WordPress strong> is a powerful tool that allows us to create fields that can contain data according to our needs. It manage as a couple “ key / value “. The key is the name that identifies the field. It is the same for all posts. The value of the key when it can vary depending on the post.
A custom field can for example be used to save a price or color data on a WordPress post. This can be very useful when the post allows for example to represent a product sheet as part of a catalog
To create a custom field, once you are on the form of a post in the administration interface of WordPress, go to the tab “Custom Field”. Then click the “Add” button to enter a key and a value.
Once the field is created, just one line of code is needed to retrieve it from our theme. This is done as follows:
<?php echo get_post_meta($post->ID, 'cle', true); ?>
N.B: ‘key’ must be replaced in the code below by the key of your personalized field. For example, if you have created a custom field ‘color’ to manage the colors of your products, the code will be
<?php echo get_post_meta($post->ID, 'couleur', true); ?>
Here are some scenarios in which you can use the custom fields:
- Contact information
- Include video
- Specific links
- Product information (model, price, quantity, ….)
For all your offshore wordpress developments, contact us strong> to get a free quote in less than 24 hours