回答您的第一个问题post meta
»bookauthor«这种方式将呼应/显示这一点。如果定义变量$id
在您的代码中,或者您可以按如下所示执行。
代码应该回答您的第二个问题,即如何通过挂钩将第二个标题行插入产品页woocommerce_single_product_summary
. 只需添加以下额外信息:
function wpse116660_wc_add_2nd_title() {
?>
<div class="2nd-tile">
<?php echo get_post_meta(get_the_ID(), "bookauthor", true); ?>
</div>
<?php
}
add_action( \'woocommerce_single_product_summary\', \'wpse116660_wc_add_2nd_title\', 6 );
为了更好地使用自定义的post meta,您可以按照@pl4g4和@brasofilo的建议,在产品编辑屏幕上添加一个metabox,但这当然不是必需的,您似乎知道如何使用标准的wordpress
custom fields metabox.
您可以这样添加元框,代码基于add_meta_box wordpress codex页面。
/**
* Adds a box to the main column on the Post and Page edit screens.
*/
function wpse116660_wc_2nd_title_mb() {
$screen = array( \'product\' );
add_meta_box(
\'wc_2nd_title_mb\',
__( \'2nd title\', \'your_textdomain\' ),
\'wc_2nd_title_inner_mb\',
$screen,
\'advanced\',
\'high\'
);
}
add_action( \'add_meta_boxes\', \'wpse116660_wc_2nd_title_mb\', 0 );
/**
* Prints the box content.
*
* @param WP_Post $post The object for the current post/page.
*/
function wpse116660_wc_2nd_title_inner_mb( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( \'wc_2nd_title_inner_mb\', \'wc_2nd_title_inner_mb_nonce\' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, \'bookauthor\', true );
echo \'<label for="bookauthor_field">\';
_e( "Bookauthor", \'your_textdomain\' );
echo \'</label> \';
echo \'<input type="text" id="bookauthor_field" name="bookauthor_field" value="\' . esc_attr( $value ) . \'" size="50" />\';
}
/**
* When the post is saved, saves our custom data.
*
* @param int $post_id The ID of the post being saved.
*/
function wpse116660_wc_2nd_title_save_postdata( $post_id ) {
/*
* We need to verify this came from the our screen and with proper authorization,
* because save_post can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST[\'wc_2nd_title_inner_mb_nonce\'] ) )
return $post_id;
$nonce = $_POST[\'wc_2nd_title_inner_mb_nonce\'];
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce, \'wc_2nd_title_inner_mb\' ) )
return $post_id;
// 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 $post_id;
// Check the user\'s permissions.
if ( \'page\' == $_POST[\'post_type\'] ) {
if ( ! current_user_can( \'edit_page\', $post_id ) )
return $post_id;
} else {
if ( ! current_user_can( \'edit_post\', $post_id ) )
return $post_id;
}
/* OK, its safe for us to save the data now. */
// Sanitize user input.
$mydata = sanitize_text_field( $_POST[\'bookauthor_field\'] );
// Update the meta field in the database.
update_post_meta( $post_id, \'bookauthor\', $mydata );
}
add_action( \'save_post\', \'wpse116660_wc_2nd_title_save_postdata\' );