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. The f...)
 
无编辑摘要
第1行: 第1行:
==Description==
==Description==
==描述==
The function, [[WordPress:Function Reference/update post meta|update post meta()]], updates the value of an existing meta key (custom field) for the specified post.
The function, [[WordPress:Function Reference/update post meta|update post meta()]], updates the value of an existing meta key (custom field) for the specified post.
函数 [[WordPress:Function Reference/update post meta|update post meta()]]为某篇文章更新现存的meta key(自定义区)。


The function returns <var>true</var> upon successful updating, and returns <var>false</var> if the post does not have the meta key specified.
The function returns <var>true</var> upon successful updating, and returns <var>false</var> if the post does not have the meta key specified.
成功更新之后,函数返回<var>true</var>,如果文章没有规定meta key,函数返回<var>false</var>。


If you want to add a new meta key and value, use the [[WordPress:Function Reference/add post meta|add_post_meta()]] function instead.
If you want to add a new meta key and value, use the [[WordPress:Function Reference/add post meta|add_post_meta()]] function instead.
如果你想要添加一个新的meta key和参数值,请使用[[WordPress:Function Reference/add post meta|add_post_meta()]]函数。


==Usage==
==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); ?> %%%
%%% <?php update_post_meta($post_id, $meta_key, $meta_value, $prev_value); ?> %%%


第12行: 第24行:


===Default Usage===
===Default Usage===
==例子==
===默认用法===
  <?php update_post_meta(76, 'my_key', 'Steve'); ?>
  <?php update_post_meta(76, 'my_key', 'Steve'); ?>
<?php update_post_meta(76, 'my_key', 'Steve'); ?>


===Other Examples===
===Other Examples===
===其它的例子===
Assuming a post has an ID of 76, and the following 4 custom fields:
Assuming a post has an ID of 76, and the following 4 custom fields:
假定一篇文章的ID是76,下面的4个自定义区:


<div style="border:1px solid blue; width:50%; margin: 20px; padding:20px">
<div style="border:1px solid blue; width:50%; margin: 20px; padding:20px">
[key_1] => 'Happy'<br/>
[key_1] => 'Happy'<br/>
[key_1] => 'Sad'<br/>
[key_1] => 'Sad'<br/>
[key_2] => 'Gregory'<br/>
[my_key] => 'Steve'<br/>
</div>
<div style="border:1px solid blue; width:50%; margin: 20px; padding:20px">
[key_1] => '愉快'<br/>
[key_1] => '悲伤'<br/>
[key_2] => 'Gregory'<br/>
[key_2] => 'Gregory'<br/>
[my_key] => 'Steve'<br/>
[my_key] => 'Steve'<br/>
第25行: 第51行:


To change <tt>key_2</tt>'s value to ''Hans'':
To change <tt>key_2</tt>'s value to ''Hans'':
将<tt>key_2</tt>的参数值更改为''Hans'':
  <?php update_post_meta(76, 'key_2', 'Hans'); ?>
  <?php update_post_meta(76, 'key_2', 'Hans'); ?>
<?php update_post_meta(76, 'key_2', 'Hans'); ?>


To change <tt>key_1</tt>'s value from ''Sad'' to ''Happy'':
To change <tt>key_1</tt>'s value from ''Sad'' to ''Happy'':
  <?php update_post_meta(76, 'key_1', 'Happy', 'Sad'); ?>
  <?php update_post_meta(76, 'key_1', 'Happy', 'Sad'); ?>
将<tt>key_1</tt>的参数值从''悲伤'' 更改为 ''愉悦'':
<?php update_post_meta(76, 'key_1', 'Happy', 'Sad'); ?>


The fields would now look like this:
The fields would now look like this:
 
Fields现在看起来像:
<div style="border:1px solid blue; width:50%; margin: 20px; padding:20px">
[key_1] => 'Happy'<br/>
[key_1] => 'Happy'<br/>
[key_2] => 'Hans'<br/>
[my_key] => 'Steve'<br/>
</div>
<div style="border:1px solid blue; width:50%; margin: 20px; padding:20px">
<div style="border:1px solid blue; width:50%; margin: 20px; padding:20px">
[key_1] => 'Happy'<br/>
[key_1] => 'Happy'<br/>
第39行: 第79行:
</div>
</div>
'''''Note:''' This function will update only the first field that matches the criteria.''
'''''Note:''' This function will update only the first field that matches the criteria.''
'''''注:'''这个函数只会更新匹配标准的第一个field。''
To change the first <tt>key_1</tt>'s value from ''Happy'' to ''Excited'':
将第一个<tt>key_1</tt>的参数值从''高兴'' 更改为 ''兴奋的'':


To change the first <tt>key_1</tt>'s value from ''Happy'' to ''Excited'':
<pre><?php  
<pre><?php  
   update_post_meta(76, 'key_1', 'Excited', 'Happy');
   update_post_meta(76, 'key_1', 'Excited', 'Happy');
第54行: 第97行:
     update_post_meta(76, 'key_1', 'Excited', $value);
     update_post_meta(76, 'key_1', 'Excited', $value);
?></pre>
?></pre>
<pre><?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);
?></pre>
For a more detailed example, go to the [[WordPress:Function Reference/post meta Function Examples|post_meta Functions Examples]] page.
For a more detailed example, go to the [[WordPress:Function Reference/post meta Function Examples|post_meta Functions Examples]] page.
关于更详细的例子,请进入[[WordPress:Function Reference/post meta Function Examples|post_meta 函数例子]]网页。
==Parameters==
==参数==


==Parameters==
{{Parameter|$post_id|integer|The ID of the post which contains the field you will edit.}}
{{Parameter|$post_id|integer|The ID of the post which contains the field you will edit.}}
{{Parameter|$meta_key|string|The key of the custom field you will edit.}}
{{Parameter|$meta_key|string|The key of the custom field you will edit.}}
{{Parameter|$meta_value|string|The new value of the custom field.}}
{{Parameter|$meta_value|string|The new value of the custom field.}}
{{Parameter|$prev_value|string|The old value of the custom field you wish to change. This is to differentiate between several fields with the same key.|optional}}
{{Parameter|$prev_value|string|The old value of the custom field you wish to change. This is to differentiate between several fields with the same key.|optional}}
{{Parameter|$post_id|integer|包含你需要编辑的field的文章的ID。}}
{{Parameter|$meta_key|string|你需要编辑的自定义field的关键字。}}
{{Parameter|$meta_value|string|自定义field的新的参数值。}}
{{Parameter|$prev_value|string|你想要更改的自定义field的旧的参数值。这是要区分几个相同关键词的field。|可选择的}}


==Related==
==Related==
==相关的==
[[WordPress:Function Reference/delete post meta|delete_post_meta()]], [[WordPress:Function Reference/get post meta|get_post_meta()]], [[WordPress:Function Reference/add post meta|add_post_meta()]], [[WordPress:Function Reference/get post custom|get_post_custom()]], [[WordPress:Function Reference/get post custom values|get_post_custom_values()]], [[WordPress:Function Reference/get post custom keys|get_post_custom_keys()]]
[[WordPress:Function Reference/delete post meta|delete_post_meta()]], [[WordPress:Function Reference/get post meta|get_post_meta()]], [[WordPress:Function Reference/add post meta|add_post_meta()]], [[WordPress:Function Reference/get post custom|get_post_custom()]], [[WordPress:Function Reference/get post custom values|get_post_custom_values()]], [[WordPress:Function Reference/get post custom keys|get_post_custom_keys()]]
[[WordPress:Function Reference/delete post meta|delete_post_meta()]], [[WordPress:Function Reference/get post meta|get_post_meta()]], [[WordPress:Function Reference/add post meta|add_post_meta()]], [[WordPress:Function Reference/get post custom|get_post_custom()]], [[WordPress:Function Reference/get post custom values|get_post_custom_values()]], [[WordPress:Function Reference/get post custom keys|get_post_custom_keys()]]

2008年7月19日 (六) 10:56的版本

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()