WordPress:Function Reference/update post meta
Description
描述
The function, update post meta(), updates the value of an existing meta key (custom field) for the specified post.
函数 update post meta()为某篇文章更新现存的meta key(自定义区)。
The function returns true upon successful updating, and returns false if the post does not have the meta key specified.
成功更新之后,函数返回true,如果文章没有规定meta key,函数返回false。
If you want to add a new meta key and value, use the add_post_meta() function instead.
如果你想要添加一个新的meta key和参数值,请使用add_post_meta()函数。
Usage
用法
%%% <?php update_post_meta($post_id, $meta_key, $meta_value, $prev_value); ?> %%%
%%% <?php update_post_meta($post_id, $meta_key, $meta_value, $prev_value); ?> %%%
Examples
Default Usage
例子
默认用法
<?php update_post_meta(76, 'my_key', 'Steve'); ?>
<?php update_post_meta(76, 'my_key', 'Steve'); ?>
Other Examples
其它的例子
Assuming a post has an ID of 76, and the following 4 custom fields:
假定一篇文章的ID是76,下面的4个自定义区:
[key_1] => 'Happy'
[key_1] => 'Sad'
[key_2] => 'Gregory'
[my_key] => 'Steve'
[key_1] => '愉快'
[key_1] => '悲伤'
[key_2] => 'Gregory'
[my_key] => 'Steve'
To change key_2's value to Hans: 将key_2的参数值更改为Hans:
<?php update_post_meta(76, 'key_2', 'Hans'); ?>
<?php update_post_meta(76, 'key_2', 'Hans'); ?>
To change key_1's value from Sad to Happy:
<?php update_post_meta(76, 'key_1', 'Happy', 'Sad'); ?>
将key_1的参数值从悲伤 更改为 愉悦:
<?php update_post_meta(76, 'key_1', 'Happy', 'Sad'); ?>
The fields would now look like this: Fields现在看起来像:
[key_1] => 'Happy'
[key_1] => 'Happy'
[key_2] => 'Hans'
[my_key] => 'Steve'
[key_1] => 'Happy'
[key_1] => 'Happy'
[key_2] => 'Hans'
[my_key] => 'Steve'
Note: This function will update only the first field that matches the criteria. 注:这个函数只会更新匹配标准的第一个field。 To change the first key_1's value from Happy to Excited:
将第一个key_1的参数值从高兴 更改为 兴奋的:
<?php update_post_meta(76, 'key_1', 'Excited', 'Happy'); //Or update_post_meta(76, 'key_1', 'Excited'); //To change all fields with the key "key_1": $key1_values = get_post_custom_values('key_1', 76); foreach ( $key1_values as $value ) update_post_meta(76, 'key_1', 'Excited', $value); ?>
<?php update_post_meta(76, 'key_1', 'Excited', 'Happy'); //或者 update_post_meta(76, 'key_1', 'Excited'); //更改所有关键字为 "key_1"的fields: $key1_values = get_post_custom_values('key_1', 76); foreach ( $key1_values as $value ) update_post_meta(76, 'key_1', 'Excited', $value); ?>
For a more detailed example, go to the post_meta Functions Examples page. 关于更详细的例子,请进入post_meta 函数例子网页。
Parameters
参数
Related
相关的
delete_post_meta(), get_post_meta(), add_post_meta(), get_post_custom(), get_post_custom_values(), get_post_custom_keys()
delete_post_meta(), get_post_meta(), add_post_meta(), get_post_custom(), get_post_custom_values(), get_post_custom_keys()