因此,基本上我需要访问checked()的值,以便在我的单模板中加载不同的模板部分。php文件。
这是我的元框:
<?php
function my_theme_add_meta_box_post_template_switcher() {
dd_meta_box( \'my_theme-post-layout\', __( \'Post template\', \'my_theme\' ), \'my_theme_show_post_template_switcher\', \'post\', \'normal\', \'high\' );
}
add_action( \'add_meta_boxes\', \'my_theme_add_meta_box_post_template_switcher\');
function my_theme_show_post_template_switcher( $post ) {
$template = get_post_meta( $post->ID, \'_my_theme_post_template\', true );
// Default template for new posts
if( empty( $template ) ) {
$template = \'default\';
}
wp_nonce_field( \'save_post_template\', \'post_template\' );
?>
<fieldset class="clearfix">
<div class="post-layout">
<label for="sidebar-left-post">
<input type="radio" id="sidebar-left-post" name="_my_theme_post_template" value="template-1" <?php checked( $template, \'template-1\' ); ?> />
<img width="150" height="100" src="<?php echo get_template_directory_uri(); ?>/admin/images/left-sidebar.png" >
<span> <?php _e( \'Sidebar left\', \'my_theme\' ); ?> </span>
</label>
</div>
<div class="post-layout">
<label for="full-width-post">
<input type="radio" id="full-width-post" name="_my_theme_post_template" value="template-2" <?php checked( $template, \'template-2\' ); ?> />
<img width="150" height="100" src="<?php echo get_template_directory_uri(); ?>/admin/images/full-width-layout.png" >
<span> <?php _e( \'Full width\', \'my_theme\' ); ?> </span>
</label>
</div>
</fieldset>
<?php
}
function my_theme_save_post_template( $post_id ) {
if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {
return;
}
if( !isset( $_POST[\'post_template\'] ) || !wp_verify_nonce( $_POST[\'post_template\'], \'save_post_template\' ) ) {
return;
}
if( !current_user_can( \'edit_post\' ) ) {
return;
}
if( isset( $_POST[\'_my_theme_post_template\'] ) ) {
update_post_meta( $post_id, \'_my_theme_post_template\', esc_attr( strip_tags( $_POST[\'_my_theme_post_template\'] ) ) );
}
}
add_action( \'save_post\', \'my_theme_save_post_template\' );
function my_theme_get_post_template_for_template_loader( $template ) {
$post = get_queried_object();
if ( $post ) {
$template = get_stylesheet_directory() . "/single.php";
}
return $template;
}
现在,我需要访问checked()值,所以在我的单子中。php我可以根据checked()的值加载不同的部分,如下所示:
switch ( $checked ) {
case checked( $template, \'template-1\' ):
get_template_part( \'inc/post-loops/template-1\' );
break;
case checked( $template, \'template-2\' ) :
get_template_part( \'inc/post-loops/template-2\' );
default:
get_template_part( \'inc/post-loops/template-1\' );
break;
}
最合适的回答,由SO网友:TheDeadMedic 整理而成
你还没有完全掌握switch
陈述read up here.
$template = get_post_meta( get_the_ID(), \'_my_theme_post_template\', true );
switch ( $template ) {
case \'template-2\':
get_template_part( \'inc/post-loops/template-1\' );
break;
case \'template-1\':
get_template_part( \'inc/post-loops/template-2\' );
break;
default:
// Neither values matched, do something else?
break;
}