This article describes how to add custom fields and related functions to the background editing area in WordPress. it briefly describes the usage of add_post_meta and add_meta_box. For more information, see
Add_post_meta
The add_post_meta function is a function used in WordPress to add custom field values to articles or pages,
Its usage is the same as adding custom field values to an article by using the custom column panel on the document writing interface.
Add_post_meta function description
Add custom fields to the document.
Common use: articles browsing times, favorite buttons, seo plug-ins and other commonly used plug-ins are the use of custom field functions.
Parameter details
add_post_meta($post_id, $meta_key, $meta_value,$unique);
$ Post_id
ID of the document or page for which you want to add a custom field
$ Meta_key
Key Value (name) of the custom field)
$ Meta_value
Custom field value
$ Unique
If a user-defined field with the same name already exists and whether to add a user-defined field with the same name again, true is not allowed, false is allowed
Function instance
// Add the _ postviews custom field to an article with ID 1. The value is 99add_post_meta (1, "_ postviews", "99"); var_dump (get_post_meta (1 )); echo"
"; // Add a _ postviews custom field to an article with ID 1. The value is 999, and the custom field name add_post_meta (1," _ postviews ", 999, false); var_dump (get_post_meta (1); echo"
";
Demo effect:
Array (1) {["_ postviews"] => array (1) {[0] => string (2) "99"} array (1) {["_ postviews"] => array (2) {[0] => string (2) "99" [1] => string (3) "999" }}// code that does not allow repeated custom fields add_post_meta (1, "_ postviews", "996", true); var_dump (get_post_meta (1 )); echo"
"; Add_post_meta (1," _ postviews "," 997 ", true); var_dump (get_post_meta (1); echo"
"; Array (1) {[" _ postviews "] => array (1) {[0] => string (3)" 996 "} array (1) {["_ postviews"] => array (1) {[0] => string (3) "996 "}}
Add_meta_box
Add_meta_box is an advanced function used by WordPress. if you can use this function, you are more familiar with this world-renowned blog program than an ordinary blogger, at least you have spent a lot of effort on it. It can be used to show that you are currently tossing your own theme, plug-in, or even tossing the WordPress background.
It seems that we have already described enough. next we will explain how to use this function from an advanced perspective.
Add_meta_box function description
The add_meta_box function is a function used to add a set region on the document editing page.
Parameter description
<?php add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args ); ?>
$ Id: Set the id attribute value in the region in the HTML code.
$ Title name in the title area
$ Callback: Display function of the setting area added (callback function)
$ Post_type is displayed on the post or page editing page.
$ Context: Set the display position of the region, including the main editing area, sidebar, and others.
$ Priority: set the priority of the region display.
$ Callback_args: Additional parameters accepted by the callback function
Use instance
Function add_xz_box () {// add the add_meta_box ('xz _ box_1 ', 'Add _ meta_box test', 'xz _ box_1', 'post ', 'Day', 'high', array ('str1', 'str2 '));}; // Mount add_xz_box function add_action ('add _ meta_boxes ', 'Add _ xz_box') on 'Add _ meta_boxes '; function xz_box_1 ($ post, $ boxargs) {// display the callback function echo "add_meta_box test ";};