WordPress函数文档add_post_meta()

添加自定义栏目(字段)到文章/页面 描述 译文 add_post_meta为指定文章添加自定义(meta)字段…

添加自定义栏目(字段)到文章/页面

描述

译文

add_post_meta为指定文章添加自定义(meta)字段。

若$unique参数设为true且已指定meta关键字,函数返回false,不作更改;否则返回true。

原文

Adds a custom field (also called meta-data) to a specified post which could be of any post type. A custom field is effectively a key–value pair.

Note that if the given key already exists among custom fields of the specified post, another custom field with the same key is added unless the $unique argument is set to true, in which case, no changes are made. If you want to update the value of an existing key, use the update_post_meta() function instead.

用法

 <?php add_post_meta($post_id$meta_key$meta_value$unique); ?> 

参数

$post_id

(integer) (必填) The ID of the post to which a custom field should be added.

默认值: None

$meta_key

(string) (必填) The key of the custom field which should be added.

默认值: None

$meta_value

(mixed) (必填) The value of the custom field which should be added. If an array is given, it will be serialized into a string.

默认值: None

$unique

(boolean) (可选) Whether or not you want the key to stay unique. When set to true, the custom field will not be added if the given key already exists among custom fields of the specified post.

默认值: false

示例

Default Usage

Adding or Updating a Unique Custom Field

Adds a new custom field if the key does not already exist, or updates the value of the custom field with that key otherwise.

Other Examples

Adds a new custom field only if a custom field with the given key does not already exists:

Adds several custom fields with different values but with the same key ‘my_key’:

For a more detailed example, see the post_meta Functions Examples page.

Hidden Custom Fields

If you are a plugin or theme developer and you are planning to use custom fields to store parameters related to your plugin or template, it is interesting to note that WordPress will not show custom fields which have keys starting with an “_” (underscore) in the custom fields list on the post edit screen or when using the the_meta() template function. This can be for example used to show these custom fields in an unusual way by using the add_meta_box() function.

The following example:

will add a unique custom field with the key name ‘_color’ and the value ‘red’ but this custom field will not display in the post edit screen.

Hidden Arrays

In addition, if the $meta_value argument is an array, it will not be displayed on the page edit screen, even if you don’t prefix the key name with an underscore.

注意

  • 使用到 add_metadata().

Character Escaping

Because meta values are passed through the stripslashes() function, you need to be careful about content escaped with characters. You can read more about the behavior, and a workaround example, in the update_post_meta() documentation.

历史

添加于 版本 1.5.0

相关

Custom Fields:
the_meta(),
get_post_meta(),
add_post_meta(),
update_post_meta(),
delete_post_meta(),
get_post_custom(),
get_post_custom_values(),
get_post_custom_keys()
(See Also: post_meta Function(函数) Examples)

  • 原文:http://codex.wordpress.org/Function_Reference/add_post_meta
类别:WordPress函数文档

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT