WordPress: Function Reference/add post meta:修訂間差異
(新頁面: ==Description== add_post_meta adds a custom (meta) field to the specified post. If the <tt>$unique</tt> parameter is set to <var>true</var>...) ? |
無編輯摘要 |
||
第1行: | 第1行: | ||
==Description== | ==Description== | ||
==描述== | |||
[[WordPress:Function Reference/add post meta|add_post_meta]] adds a custom (meta) field to the specified post. | [[WordPress:Function Reference/add post meta|add_post_meta]] adds a custom (meta) field to the specified post. | ||
[[WordPress:Function Reference/add post meta|add_post_meta]]為某篇文章添加了一個自定義的(meta)區(qū)。 | |||
If the <tt>$unique</tt> parameter is set to <var>true</var> and the specified meta key already exists, the function returns <var>false</var> and makes no changes; otherwise, it returns <var>true</var>. | If the <tt>$unique</tt> parameter is set to <var>true</var> and the specified meta key already exists, the function returns <var>false</var> and makes no changes; otherwise, it returns <var>true</var>. | ||
如果<tt>$unique</tt>參數(shù)設置為<var>true</var>而且已經存在特別的meta關鍵詞,函數(shù)返回<var>false</var>而且不做什么更改;否則的話,函數(shù)返回<var>true</var>。 | |||
==Usage== | ==Usage== | ||
==用法== | |||
%%% <?php add_post_meta($post_id, $meta_key, $meta_value, $unique); ?> %%% | %%% <?php add_post_meta($post_id, $meta_key, $meta_value, $unique); ?> %%% | ||
%%% <?php add_post_meta($post_id, $meta_key, $meta_value, $unique); ?> %%% | |||
==Examples== | ==Examples== | ||
==例子== | |||
===Default Usage=== | ===Default Usage=== | ||
===默認用法=== | |||
? <?php add_post_meta($68, 'my_key', 47); ?> | ? <?php add_post_meta($68, 'my_key', 47); ?> | ||
<?php add_post_meta($68, 'my_key', 47); ?> | |||
===Adding or updating a unique field=== | ===Adding or updating a unique field=== | ||
===添加或者更新一個獨特的區(qū)=== | |||
Adds a new field if the key doesn't exist, or updates the existing field. | Adds a new field if the key doesn't exist, or updates the existing field. | ||
如果你存在關鍵詞,添加一個新的field,或者更新現(xiàn)存的field。 | |||
?? <?php add_post_meta(7, 'fruit', 'banana', true) or update_post_meta(7, 'fruit', 'banana'); ?> | ?? <?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=== | ===Other Examples=== | ||
===其它的例子=== | |||
If you wanted to make sure that there were no fields with the key ''"my_key"'', before adding it: | 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); ?> | ? <?php add_post_meta(68, 'my_key', '47', true); ?> | ||
To add several values to the key ''"my_key"'': | To add several values to the key ''"my_key"'': | ||
第24行: | 第50行: | ||
<?php add_post_meta(68, 'my_key', 'The quick, brown fox jumped over the lazy dog.'); ?> | <?php add_post_meta(68, 'my_key', 'The quick, brown fox jumped over the lazy dog.'); ?> | ||
...</pre> | ...</pre> | ||
<?php add_post_meta(68, 'my_key', '47', true); ?> | |||
To add several values to the key ''"my_key"'': | |||
<pre><?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.'); ?> | |||
...</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 函數(shù)例子]]網頁。 | |||
=== Making a "Hidden" Custom Field === | === Making a "Hidden" Custom Field === | ||
=== 制作一個 "隱藏的" 自定義區(qū) === | |||
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. | 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. | ||
如果你是一個插件/主題開發(fā)人員而且你計劃使用自定義field儲存與你的插件或者主題相關的參數(shù),你或饒有興趣地發(fā)現(xiàn),WordPress習慣于顯示網頁/文章編輯網頁中自定義field列表上以"_"(下劃線)開始的關鍵字。就是說,在自定義參數(shù)中將下劃線設置為第一個字符,是個很好的做法,這樣你可以將設置儲存為自定義fields,但是你的設置不會在管理UI中的自定義fields列表中顯示。 | |||
The following example: | The following example: | ||
下面的例子: | |||
? <?php add_post_meta(68, '_color', 'red', true); ?> | ? <?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. | 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" 的自定義范圍而且這個自定義范圍中的參數(shù)值"red"不會在網頁/文章編輯網頁中顯示。 | |||
==Parameters== | ==Parameters== | ||
==參數(shù)== | |||
{{Parameter|$post_id|integer|The ID of the post to which you will add a custom field.}} | {{Parameter|$post_id|integer|The ID of the post to which you will add a custom field.}} | ||
{{Parameter|$meta_key|string|The key of the custom field you will add.}} | {{Parameter|$meta_key|string|The key of the custom field you will add.}} | ||
{{Parameter|$meta_value|string|The value of the custom field you will add.}} | {{Parameter|$meta_value|string|The value of the custom field you will add.}} | ||
{{Parameter|$unique|boolean|Whether or not you want the key to be unique. When set to true, this will ensure that there is not already a custom field attached to the post with <tt>$meta_key</tt> as it's key, and, if such a field already exists, the key will not be added.|optional|false}} | {{Parameter|$unique|boolean|Whether or not you want the key to be unique. When set to true, this will ensure that there is not already a custom field attached to the post with <tt>$meta_key</tt> as it's key, and, if such a field already exists, the key will not be added.|optional|false}} | ||
{{Parameter|$post_id|integer|你添加一個自定義范圍的文章的ID。}} | |||
{{Parameter|$meta_key|string|你將要添加的自定義field的關鍵詞。}} | |||
{{Parameter|$meta_value|string|你將要添加的自定義field的參數(shù)值。}} | |||
{{Parameter|$unique|boolean|你是否想要獨特的關鍵字。如果設置為真的,能夠確保不存在文章的關鍵字with <tt>$meta_key</tt> 如果這個field已經存在,這個關鍵詞就不會得到添加。|optional|false}} | |||
==Related== | ==Related== | ||
==相關的== | |||
[[WordPress:Function Reference/delete post meta|delete_post_meta()]], [[WordPress:Function Reference/get post meta|get_post_meta()]], [[WordPress:Function Reference/update post meta|update_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/update post meta|update_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/update post meta|update_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:39的版本
Description
描述
add_post_meta adds a custom (meta) field to the specified post. add_post_meta為某篇文章添加了一個自定義的(meta)區(qū)。
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參數(shù)設置為true而且已經存在特別的meta關鍵詞,函數(shù)返回false而且不做什么更改;否則的話,函數(shù)返回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
添加或者更新一個獨特的區(qū)
Adds a new field if the key doesn't exist, or updates the existing field.
如果你存在關鍵詞,添加一個新的field,或者更新現(xiàn)存的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 函數(shù)例子網頁。
Making a "Hidden" Custom Field
制作一個 "隱藏的" 自定義區(qū)
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.
如果你是一個插件/主題開發(fā)人員而且你計劃使用自定義field儲存與你的插件或者主題相關的參數(shù),你或饒有興趣地發(fā)現(xiàn),WordPress習慣于顯示網頁/文章編輯網頁中自定義field列表上以"_"(下劃線)開始的關鍵字。就是說,在自定義參數(shù)中將下劃線設置為第一個字符,是個很好的做法,這樣你可以將設置儲存為自定義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" 的自定義范圍而且這個自定義范圍中的參數(shù)值"red"不會在網頁/文章編輯網頁中顯示。
Parameters
參數(shù)
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()