![]() ![]() Result: Display multi-data in a custom field To display the value Soft, we edit the code as below: In post Quite, because our parameter $single is true, custom field Type returns a first value ("Hard") only while it has 2 ones. The result will be like this: Display single custom field You can use get_the_ID() to get the ID of the current post if you are in the loop.įor example: Replace the the_meta() paragraph by the below code: $single: Define the type of data, true to return a single value and false to return an array of all values.$key: The key of the custom field, also is the value of column Name.$post_id: The ID of the post you want to get custom fields from.Use the get_post_meta() function to get custom field value: get_post_meta( $post_id, $key, $single ) ![]() You must get the value of each custom field and display them by yourself. In the post-Quiet, we added 2 values into the custom field Type, so these values are separated by a comma (,). Now our homepage will be like this: Homepage after adding custom fields I want the list of custom fields displays right after the Post Title in both archive and single page, so I add the_meta() function into the file template-parts/post/content.php as below: Here we go! Display the List of Custom Fields - Disadvantage: You need to take more time to learn about the child theme.īecause we are concentrating on custom fields only, I choose to edit the theme directly.- Advantage: Changes will NOT be lost when you upgrade the theme.- Disadvantage: Changes will be lost when you upgrade the theme.You also need to change the code in your theme with 2 following options: You have to call these functions in the loop. ![]() get_post_custom_values( $key ): returns an array of all values of a custom field $key.get_post_custom_keys(): returns an array of all custom fields’ keys.get_post_custom(): gets all values of custom fields in a key/value array.get_post_meta( $post_id, $key, $single ): gets the value of a custom field.the_meta(): shows the list of custom fields in the front-end in an unordered list.Handling by yourself a custom field from UI to data saving is much more complicated. So you will consider that it is convenient to use? It is the trade-off. If you have no idea about coding, you will grope in a half-day maybe. This feature is also called cloning fields in Meta Box, or repeater fields in other plugins. If you add data into an existing custom field, it will save both values. This is an example of saving information about a product with Price and Type are text and image encoded in base64 of an SVG file: An example of saving the information about a product in custom fields Save Multiple Values in a Custom FieldĪfter saving the post, you will see an additional box to choose a name for the custom fields instead of creating a new one. Data encoded in base64 of image, file, and so on.It can save whatever data can be converted to text: Value: it is value in the simple text of the custom field.Īlthough it is a text field, it is quite flexible.Name: it is an arbitrary name for the custom field.This form is quite simple, as your thought, the meaning of columns are as below: In the Custom Fields area, you’ll see the fields to fill data. ![]() If you want it supports custom post types, you must declare it in option support when you register custom post types: 'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ) Now you will see a box of Custom Fields under the editor area as below: The Custom Fields areaīy default, this function works with Posts only. Step 2: Check the box "Custom Fields" if it has been ticked or not. Step 1: Go to add a new post or edit a post, then click on Screen Options. Let's get started to find them out! How to Enable the Built-In Function of Custom Fields? You may add as many fields as you want with any names you like. We have said in the latest post that WordPress has a default function that helps end-users manage custom fields in a simple way. How to Enable the Built-In Function of Custom Fields? ![]()
0 Comments
Leave a Reply. |