add_post_meta
The add_post_meta function is a function in WordPress used to add custom field values to posts or pages,
Its usage is the same as using the custom column panel to add custom field values to the article in the article writing interface when writing an article.
add_post_meta function description
Add custom fields to articles.
Common uses include: article views, like buttons, seo plug-ins and other commonly used plug-ins are the custom field functions used.
Detailed explanation of parameters
add_post_meta($post_id, $meta_key, $meta_value,$unique);
$post_id
The ID value of the post or page to which the custom field is to be added
$meta_key
Key value (name) of custom field
$meta_value
Custom field value
$unique
If there is already a custom field with the same name, whether to add a custom field with the same name repeatedly, true means not allowed, false means allowed
Function usage examples
//为ID为1的文章添加_postviews自定义字段,值为99 add_post_meta(1, "_postviews", "99"); var_dump(get_post_meta(1));echo"<br />"; //为ID为1的文章添加_postviews自定义字段,值为999,并允许重复自定义字段名称 add_post_meta(1, "_postviews", 999,false); var_dump(get_post_meta(1));echo"<br />";
Demo effect:
array(1) { ["_postviews"]=> array(1) { [0]=> string(2) "99" } } array(1) { ["_postviews"]=> array(2) { [0]=> string(2) "99" [1]=> string(3) "999" } } //不允许重复自定义字段的代码 add_post_meta(1, "_postviews", "996",true); var_dump(get_post_meta(1));echo"<br />"; add_post_meta(1, "_postviews", "997",true); var_dump(get_post_meta(1));echo"<br />"; 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 a function used in advanced WordPress. Being able to use this function means that you already understand this world-famous blogging program better than an ordinary blogger. At least you have spent a lot of effort on it. . Being able to use it means that you are now working on a theme, plug-in, or even the WordPress backend.
It seems that we have gone into too much detail. Let's 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 area on pages such as article editing.
Parameter description
<?php add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args ); ?>
$id Set the value of the id attribute in the area in the HTML code
Title name in $title area
$callback adds the display function (callback function) of the setting area
$post_type is displayed in the edit page of post or page
$context sets the display position of the area, main editing area, sidebar, others
$priority sets the priority of area display
$callback_args Additional parameters accepted by the callback function
Usage examples
function add_xz_box (){//添加设置区域的函数 add_meta_box('xz_box_1', 'add_meta_box 测试', 'xz_box_1','post','side','high',array('str1','str2')); }; //在'add_meta_boxes'挂载 add_xz_box 函数 add_action('add_meta_boxes','add_xz_box'); function xz_box_1($post,$boxargs){//显示设置区域的回调函数 echo"add_meta_box 测试"; };