您可以为此添加自定义元字段。
下面是为post类型添加元字段的示例代码post
. 有关更多详细信息,请参阅link.
<?php
/************************************************************************/
function myPrice_meta_box()
{
add_meta_box(\'myPrice_field\', \'Price\', \'myPrice_meta_box_callback\', \'post\', \'normal\', \'high\');
}
add_action(\'add_meta_boxes\', \'myPrice_meta_box\');
/**
* Prints the box content.
*
* @param WP_Post $post The object for the current post/page.
*/
function myPrice_meta_box_callback($post)
{
global $post;
// Add a nonce field so we can check for it later.
wp_nonce_field(\'myPrice_meta_box\', \'myPrice_meta_box_nonce\');
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$item_Price = get_post_meta($post->ID, \'_price_\', true);
?>
<p class="order_id">
<span>
<label>Price::</label>
<input type="text" name="_price_" size="50" value="<?php echo $item_Price ?>" placeholder="Price">
</span><br/>
</p>
<?php
}
/**
* When the post is saved, saves our myPrice data.
* @param int $post_id The ID of the post being saved.
*/
function myPrice_meta_box_data($post_id)
{
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
/* Check if our nonce is set.*/
if (!isset($_POST[\'myPrice_meta_box_nonce\']))
{
return;
}
/* Verify that the nonce is valid.*/
if (!wp_verify_nonce($_POST[\'myPrice_meta_box_nonce\'], \'myPrice_meta_box\'))
{
return;
}
/* If this is an autosave, our form has not been submitted, so we don\'t want to do anything.*/
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
{
return;
}
$price = isset($_POST["_price_"] ) ? $_POST["_price_"] : \'\';
/* Update the meta field in the database.*/
update_post_meta($post_id, \'_price_\', $price);
}
add_action(\'save_post\', \'myPrice_meta_box_data\');
?>