Add custom fields and background editing functional areas to articles in WordPress, _PHP tutorial

WBOY
Release: 2016-07-12 09:02:33
Original
1028 people have browsed it

Add custom fields and background editing function areas to articles in WordPress,

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);
Copy after login

$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 />";
Copy after login

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"
 }
}

Copy after login

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.

20151219173817018.jpg (299×335)

Parameter description

<&#63;php
 add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args );
 &#63;>
Copy after login

$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 测试";
};
Copy after login

Articles you may be interested in:

  • A collection of practical WordPress backend MySQL operation commands
  • About the solution for changing the WordPress backend to English on bluehost space
  • Share code examples for adding prompt boxes to the WordPress editing backend

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1084594.htmlTechArticleAdd custom fields and background editing functional areas to articles in WordPress. The add_post_meta add_post_meta function is used in WordPress to add custom fields to articles. Or a function to add custom field values ​​to the page...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!