get_post_meta() This is a WordPress function used to retrieve custom fields for articles or pages; it is also known as "metadata." Its function prototype is as follows:
get_post_meta( int $post_id, string $key = '', bool $single = false )Parameter description:
$post_idRequired: The ID of the article or page.$keyOptional. The name of the custom field; if not specified, all custom fields will be returned.$singleOptional: Whether to return only a single value. The default is `false`, which means returning an array of all values; if set to `true`, only a single value is returned.
Call get_post_meta() The function can retrieve custom field data for a specific article or page. For example, if an article or page contains a custom field named "author", you can use the following code to retrieve its value:
$author = get_post_meta( get_the_ID(), 'author', true );In the above code,get_the_ID() Used to retrieve the ID of the current article or page; the second parameter is the name of the custom field; the third parameter is `true`, indicating that only a single value should be returned.
Note that the return type of a custom field value depends on the data type you used when adding the custom field. If the custom field value you added is of string type, then get_post_meta() Returns a string; if the value is of numeric type, it returns a number. If the custom field value is an array,get_post_meta() The function also returns an array.
In WordPress, custom fields can be used to store various types of data, such as the author, date, source, and more for posts or pages. You can find the "Custom Fields" option on the post or page edit page, where you can add or modify the values of custom fields. get_post_meta() The function can easily retrieve data from these custom fields.