我试图根据存储的元值包括一个特定的模板,例如:
如果post meta值为value1
, 我想包括不同的模板value2
, 我想包括不同的模板等。
以下是我的完整代码:
function wdm_add_meta_box() {
add_meta_box(
\'metabox_custom\', \'Plugin Options\', \'wdm_meta_box_callback\', \'product\',\'normal\',\'high\');
}
add\\u操作(“add\\u meta\\u box”、“wdm\\u add\\u meta\\u box”);
函数wdm\\U meta\\U box\\U回调($产品){
// Add an nonce field so we can check for it later.
wp_nonce_field( \'wdm_meta_box\', \'wdm_meta_box_nonce\' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $product->ID, \'my_key\', true );//my_key is a meta_key. Change it to whatever you want
?>
<label for="wdm_new_field"><?php _e( "Enable Design Panel:", \'choose_value\' ); ?></label>
<br />
<input type="radio" name="tailor_select" value="value1" <?php checked( $value, \'value1\' ); ?> >Value1<br>
<input type="radio" name="tailor_select" value="value2" <?php checked( $value, \'value2\' ); ?> >Value2<br>
<input type="radio" name="tailor_select" value="value3" <?php checked( $value, \'value3\' ); ?> >Value3<br>
<input type="radio" name="tailor_select" value="value4" <?php checked( $value, \'value4\' ); ?> >Value4<br>
<input type="radio" name="tailor_select" value="value5" <?php checked( $value,\'value5\'); ?> >Value5<br>
<input type="radio" name="tailor_select" value="value6" <?php checked( $value,\'value6\'); ?> >Value6<br>
<?php
}
函数wdm\\U save\\U meta\\U box\\U data($product\\U 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[\'wdm_meta_box_nonce\'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( !wp_verify_nonce( $_POST[\'wdm_meta_box_nonce\'], \'wdm_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;
}
// Check the user\'s permissions.
if ( !current_user_can( \'edit_post\', $product_id ) ) {
return;
}
// Sanitize user input.
$new_meta_value = ( isset( $_POST[\'tailor_select\'] ) ? sanitize_html_class( $_POST[\'tailor_select\'] ) : \'\' );
// Update the meta field in the database.
update_post_meta( $product_id, \'my_key\', $new_meta_value );
}
添加操作(“save\\u post”、“wdm\\u save\\u meta\\u box\\u data”);
函数choose\\u选项($模板){
$has_customization = get_post_meta( $post->ID, \'my_key\', true );
echo $has_customization;
if( $has_customization == \'value2\')
{
function wdm_load_template($template)
{
$template_slug = basename(rtrim( $template, \'.php\' ));
if( ($template_slug === \'single-product\' || $template_slug === \'woocommerce\') && is_product() )
{
$template = WCM_DIR . \'includes/wcm-templates.php\';
}
return $template;
}
add_filter( \'template_include\', \'wdm_load_template\', 99);
}
}
添加\\u操作(“wp”,“选择\\u选项”);