Add custom fields and display in posts – Learn WordPress from a to z

Tutorials 0 lượt xem

What is a WordPress Custom Field?

These are the data used to add additional information related to the post or page you are editing.

By default, when you write a new post, page or any type of content, WordPress automatically saves additional related data such as author, date, time, etc. These data are collectively known as meta. . You can also create and store your own meta using custom fields.

The purpose of creating meta is to add short information to your article. The brief information you want is separate from the article content.

Add custom fields in posts

By default, the custom field option is hidden on the post edit screen. To view it, you need to click on the Options link  shown at the top, then tick  the Custom Fields box

Then scroll down, you will see a  Custom Fields area . Maybe it will give you a name and a value, sometimes it will ask you to choose. If so, just choose any name and enter a value for it. Then press the  Add Custom Fields button

Now, I can change the name to my liking. And this name will also be saved in the custom field, when you write another post, you will see it. Note that names should be written immediately or use a “-” sign to link words. In short, do not use spaces in names .

In order for the custom field to display outside of posts, you must add the following code to your theme’s post display file, usually single.php . To know more exactly how to edit the interface, you can refer to ‘s theme programming series.

<?php echo get_post_meta($post->ID, 'key', true); ?&gt;
<div><span>1</span></div>

And this is the result:

summary

Custom fields are often used by very few people, but in some special websites it is very necessary. In particular, this one can be used in the product. You can add relevant information to the product with a custom field, such as adding a user manual link for the product.

Bài viết liên quan