WordPress:Function Reference/add post meta

来自站长百科
Fludlen讨论 | 贡献2008年7月19日 (六) 10:39的版本
跳转至: 导航、​ 搜索

Description

描述

add_post_meta adds a custom (meta) field to the specified post. add_post_meta为某篇文章添加了一个自定义的(meta)区。


If the $unique parameter is set to true and the specified meta key already exists, the function returns false and makes no changes; otherwise, it returns true.

如果$unique参数设置为true而且已经存在特别的meta关键词,函数返回false而且不做什么更改;否则的话,函数返回true

Usage

用法

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

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

Examples

例子

Default Usage

默认用法

<?php add_post_meta($68, 'my_key', 47); ?>

<?php add_post_meta($68, 'my_key', 47); ?>

Adding or updating a unique field

添加或者更新一个独特的区

Adds a new field if the key doesn't exist, or updates the existing field.

如果你存在关键词,添加一个新的field,或者更新现存的field。

 <?php add_post_meta(7, 'fruit', 'banana', true) or update_post_meta(7, 'fruit', 'banana'); ?>
 <?php add_post_meta(7, 'fruit', 'banana', true) 或者update_post_meta(7, 'fruit', 'banana'); ?>

Other Examples

其它的例子

If you wanted to make sure that there were no fields with the key "my_key", before adding it:

如果你想要确定没有带有关键字"my_key"的fieled,添加之前:

<?php add_post_meta(68, 'my_key', '47', true); ?>

To add several values to the key "my_key":

<?php add_post_meta(68, 'my_key', '47'); ?>
<?php add_post_meta(68, 'my_key', '682'); ?>
<?php add_post_meta(68, 'my_key', 'The quick, brown fox jumped over the lazy dog.'); ?>
...


<?php add_post_meta(68, 'my_key', '47', true); ?> To add several values to the key "my_key":

<?php add_post_meta(68, 'my_key', '47'); ?>
<?php add_post_meta(68, 'my_key', '682'); ?>
<?php add_post_meta(68, 'my_key', 'The quick, brown fox jumped over the lazy dog.'); ?>
...


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

更详细的例子,请进入post_meta 函数例子网页。

Making a "Hidden" Custom Field

制作一个 "隐藏的" 自定义区

If you are a plugin / 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 wont show keys starting with an "_" (underscore) in the custom fields list at the page / post editing page. That being said, it is a good practice to use an underscore as the first character in your custom parameters, that way your settings are stored as custom fields, but they won't display in the custom fields list in the admin UI.

如果你是一个插件/主题开发人员而且你计划使用自定义field储存与你的插件或者主题相关的参数,你或饶有兴趣地发现,WordPress习惯于显示网页/文章编辑网页中自定义field列表上以"_"(下划线)开始的关键字。就是说,在自定义参数中将下划线设置为第一个字符,是个很好的做法,这样你可以将设置储存为自定义fields,但是你的设置不会在管理UI中的自定义fields列表中显示。

The following example:

下面的例子:

<?php add_post_meta(68, '_color', 'red', true); ?>

will add a unique custom field with the key name "_color" and the value "red" but this custom field will not display in the page / post editing page.

<?php add_post_meta(68, '_color', 'red', true); ?> 会添加一个关键字是 "_color" 的自定义范围而且这个自定义范围中的参数值"red"不会在网页/文章编辑网页中显示。

Parameters

参数

Related

相关的

delete_post_meta(), get_post_meta(), update_post_meta(), get_post_custom(), get_post_custom_values(), get_post_custom_keys()

delete_post_meta(), get_post_meta(), update_post_meta(), get_post_custom(), get_post_custom_values(), get_post_custom_keys()