WordPress to add custom fields to the article and background editing function area _php Tips

Source: Internet
Author: User

Add_post_meta
the Add_post_meta function is a function used in WordPress to add custom field values to an article or page.
Its usage is the same as the effect of adding custom field values to an article using a custom column panel in the writing interface when writing an article.

Add_post_meta function Description
Add custom fields to the article.
Common uses are: article browsing times, like buttons, SEO plug-ins, such as commonly used plug-ins is the use of custom field functions.

Parameter detailed

Add_post_meta ($post _id, $meta _key, $meta _value, $unique);

$post _id
The ID value of the article or page to which you want to add a custom field
$meta _key
The key value (first name) of the custom field
$meta _value
Values for custom fields
$unique
If you already have a custom field with the same name, do you want to repeat the custom field with duplicate names, true to disallow, false to allow
Use instances of a function

Add the _postviews custom field for the article with ID 1, the value is
Add_post_meta (1, "_postviews", "the");
Var_dump (Get_post_meta (1)); echo "<br/>";
Add a _postviews custom field for the article with ID 1, a value of 999, and allow duplicate custom field names
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) "" "
 }

Array (1) {
 [ "_postviews"]=>
 Array (2) {
  [0]=>
  string (2) "" "
  [1]=>
  string (3)" 999 "
 }
}


Code Add_post_meta for repeating custom fields is not allowed
(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 of WordPress advanced, you can use this function that means you already know more about the world's most popular blogging program than a regular blogger, at least you've spent a lot of effort on it. can use it, that you are now tossing a theme of your own, plug-ins, or even in the background of the toss WordPress.
Seems to have been more than enough, let's take an advanced point of view to explain how this function is used.

Add_meta_box function Description
The Add_meta_box function is a function that is used to add a set area to a page such as an article edit.

Parameter description

<?php
 Add_meta_box ($id, $title, $callback, $post _type, $context, $priority, $callback _args);
 >

To set the value of an id attribute in a zone $id HTML code
Title name in the $title area
$callback the Display function (callback function) of the Settings area added
$post _type is displayed on the edit page of post or page
$context set the display location of the area, the main edit area, sidebar, other
$priority set the priority of the area display
$callback additional parameters accepted by the _args callback function
Working with instances

function Add_xz_box () {//Add functions for Settings area
add_meta_box (' xz_box_1 ', ' add_meta_box test ', ' xz_box_1 ', ' post ', ' side ', ' High ', Array (' str1 ', ' str2 '));
On the ' add_meta_boxes ' Mount Add_xz_box function
add_action (' add_meta_boxes ', ' Add_xz_box ');
 
 
function Xz_box_1 ($post, $boxargs) {//Display the callback function for the settings area
 echo "Add_meta_box test";
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.