下面是我正在使用的元盒代码,它对我来说运行良好:
// Add meta boxes with TinyMCE via wp_editor() function
// Define the custom box
add_action( \'add_meta_boxes\', \'product_details_add\' );
// Do something with the data entered
add_action( \'save_post\', \'product_details_save\' );
// Adds a box to the main column on the Product post_type edit screens
function product_details_add() {
add_meta_box( \'product_details\', \'Product Details\', \'product_details_call\', \'product\', \'normal\', \'high\' );
}
// Prints the box content
function product_details_call( $post ) {
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), \'product_details_noncename\' );
$field_value = get_post_meta( $post->ID, \'product_details_meta\', false );
wp_editor( $field_value[0], \'product_details_meta\' );
}
// When the post is saved, saves our custom data
function product_details_save( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( ( isset ( $_POST[\'product_details_noncename\'] ) ) && ( ! wp_verify_nonce( $_POST[\'product_details_noncename\'], plugin_basename( __FILE__ ) ) ) )
return;
// Check permissions
if ( ( isset ( $_POST[\'post_type\'] ) ) && ( \'page\' == $_POST[\'post_type\'] ) ) {
if ( ! current_user_can( \'edit_page\', $post_id ) ) {
return;
}
}
else {
if ( ! current_user_can( \'edit_post\', $post_id ) ) {
return;
}
}
// OK, we\'re authenticated: we need to find and save the data
if ( isset ( $_POST[\'product_details_meta\'] ) ) {
update_post_meta( $post_id, \'product_details_meta\', $_POST[\'product_details_meta\'] );
}
}
////////////
然而,
get_post_meta()
没有返回模板页面中的数据,因此我使用更“直接”的方法来获取和显示元内容:
<?php echo $product->product_custom_fields[\'product_details_meta\'][0];?>
当然,这假设您还声明
global $product;
模板文件中此代码上方的某个位置。