WIKI使用導(dǎo)航
站長百科導(dǎo)航
站長專題
- 網(wǎng)站推廣
- 網(wǎng)站程序
- 網(wǎng)站賺錢
- 虛擬主機
- cPanel
- 網(wǎng)址導(dǎo)航專題
- 云計算
- 微博營銷
- 虛擬主機管理系統(tǒng)
- 開放平臺
- WIKI程序與應(yīng)用
- 美國十大主機
WordPress:Function Reference/add post meta
Description
add_post_meta adds a custom (meta) field to the specified post.
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.
Usage
%%% <?php add_post_meta($post_id, $meta_key, $meta_value, $unique); ?> %%%
Examples
Default Usage
<?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.
<?php add_post_meta(7, 'fruit', 'banana', true) or 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:
<?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.
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.
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.
Parameters
Related
delete_post_meta(), get_post_meta(), update_post_meta(), get_post_custom(), get_post_custom_values(), get_post_custom_keys()