In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to write custom storage fields in WordPress. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
WordPress custom field is the meta information (meta information) of the article, using this function, you can extend the function of the article, is the necessary knowledge to learn WordPress plug-in development and theme development, convenient to store some additional custom content for the article.
Custom fields are not only for plug-in developers to use, WordPress feature images, custom page templates and other features of the information stored in the custom field form inside.
In the article editor interface, the "Custom Column" section can manage fields except those whose names start with "_"(if there is no "Custom Column" section, please open the "Display Options" in the upper right corner), so many custom fields used by WordPress themselves cannot be managed here.
storage principle
Custom fields are very flexible. First of all, let's talk about its storage principle and why it is so flexible. In the database, the wp_posts form for storing articles has only the default 20 items for storing the necessary article information.
The wp_postmeta table that stores custom fields has only four items, namely meta_id (ID of custom field), post_id (ID of article to which custom field belongs), meta_key (name of custom field) and meta_value (value of custom field). Generally, we only need to pay attention to meta_key and meta_value.
Because of this, every article can have custom fields with the same name but different values, and there can be unlimited custom fields, any plug-in and theme can use custom fields to extend the article information.
The name of a custom field in an article can be repeated.
add fields
Custom fields can be manipulated on the article editing page, but this article is mainly about development.
To add custom fields, you can use the add_post_meta() function:
add_post_meta( $post_id, $meta_key, $meta_value, $unique );
Parameters:
$post_id
(integer)(required) ID of article adding custom field.
Default: None
$meta_key
(String)(Required) Name of custom field.
Default: None
$meta_value
(Mixed)(Required) Custom field values.
Default: None
$unique
(Boolean)(Optional) If there is already a field with the same name, whether to add it repeatedly. True Allowed;False Not Allowed.
Default: False
update field
To update the value of a field, you can use the update_post_meta() function:
update_post_meta( $post_id, $meta_key, $meta_value, $prev_value );
Parameters:
$post_id
(integer)(required) Update ID of custom field article.
Default: None
$meta_key
(String)(Required) Name of the custom field updated.
Default: None
$meta_value
(Mixed)(Required) Updated custom field values.
Default: None
$prev_value
(Mixed)(Optional) This parameter is useful only if there are multiple custom fields with the same name in an article. If left blank, all fields with the same name are updated, otherwise fields with the same value as this parameter are updated.
Default: Empty string
get field
You can get fields using the get_post_meta() function:
get_post_meta( $post_id, $key, $single );
Parameters:
$post_id
(integer)(required) To get the article ID of the field, if in a loop, you can set it with get_the_ID().
Default: None
$key
(String)(Optional) The name of the field to get.
Default: None
$single
(Boolean)(Optional) Returns a string if True; returns an array if False, and custom field values of the same key are combined into a sequential array in the order they are added.
Default: False
example
Here is a simple example of browsing statistics:
function Bing_statistics_visitors( $cache = false ){ if( ! is_singular() ) return; global $post; $id = $post->ID; if( get_post( $id )->post_status != 'publish' ) return; $post_views = (int) get_post_meta( $id, 'views', true ); update_post_meta( $id, 'views', ( $post_views + 1 ) ) || add_post_meta( $id, 'views', 1, true );}add_action( 'wp_head', 'Bing_statistics_visitors' );
Get Count:
function Bing_get_views(){ global $post; $views = number_format( (int) get_post_meta( $post->ID, 'views', true ) ); return $views;}
invisible custom field
Although there are many custom fields created by WordPress itself in the database, they are not displayed in the "Custom Column" of the article editor (if there is no "Custom Column" section, please turn on the "Display Options" in the upper right corner), such as featured images, custom page templates and comments.
If you look closely, you will find that the names of these custom fields all start with an underscore "_", so custom fields that start with an underscore "_" will not be displayed to the user, and the_meta() function will not output, which is hidden from the user.
With this feature, we can put fields that we don't want users to modify casually, fields that will confuse users, such as setting options that already provide a setting interface, cache data, etc., to avoid errors.
Here is a small example:
add_post_meta( get_the_ID(), '_time_diff', time() );
In addition, if the custom field stores an array, even if the name is not preceded by an underscore "_", it will not be displayed.
Thank you for reading! About "how to write custom storage fields in WordPress" This article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.