我有一个自定义的元框,有两个不同的单选按钮可以选择。一个用于以“大”格式显示内容,另一个用于在前端以“小”格式显示内容。
我已经知道如何检索元框的数据并将其输出,但不知道如何“检查”选择了哪些特定数据,以及如何根据选择的值进行输出。(It should be noted that I will be using this data to output a specific CSS class that can then be applied to a different block of code designated for that selection)
以下是我的元框(更新版):
<?php
// Register Custom Post Type Meta Box for EPR Music Releases CPT -- Album Sizes
add_action( \'add_meta_boxes\', \'musicreleases_albumsize_meta_box_add\' );
function musicreleases_albumsize_meta_box_add()
{
add_meta_box( \'musicreleases_albumsize_meta_id\', \'Album Size\', \'musicreleases_albumsize_meta_box_cb\', \'eprmusicrelease\', \'normal\', \'low\' );
}
function musicreleases_albumsize_meta_box_cb( $post )
{
$value = get_post_meta( $post->ID,\'meta_box_musicreleases_albumsize\',true );
wp_nonce_field( \'my_meta_box_nonce\', \'meta_box_nonce\' );
?>
<p>
<input type="radio" name="albumsize" value="album_small" <?php echo ($value == \'album_small\')? \'checked="checked"\':\'\'; ?>/> Small<br />
<input type="radio" name="albumsize" value="album_large" <?php echo ($value == \'album_large\')? \'checked="checked"\':\'\'; ?>/> Large<br />
</p>
<?php
}
add_action( \'save_post\', \'musicreleases_albumsize_meta_box_save\' );
function musicreleases_albumsize_meta_box_save( $post_id ){
if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST[\'meta_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'meta_box_nonce\'], \'my_meta_box_nonce\' ) ) return;
if( !current_user_can( \'edit_post\' ) ) return;
//accepted values whitelist
$allowed = array(\'album_small\',\'album_large\');
if( isset( $_POST[\'albumsize\'] ) && in_array($_POST[\'albumsize\'], $allowed))
update_post_meta( $post_id, \'meta_box_musicreleases_albumsize\', $_POST[\'albumsize\'] );
}
?>
。。。如您所见,元框允许用户选择相册是显示为大版本还是小版本。
This is my current template which I need to add this data to for the front end (UPDATED):
<?php
/*
Template Name: Music Releases
*/
?>
<div class="widget_wrapper">
<h2 class="widgettitle">Releases</h2>
<?php $music_releases = new WP_Query(array(\'post_type\' => \'eprmusicrelease\')); ?>
<?php if($music_releases->have_posts()): while ($music_releases->have_posts() ) : $music_releases->the_post(); ?>
<?php if($album_url = get_post_meta($post->ID, \'meta_box_musicreleases_url\', true)): ?>
<?php $album_size = get_post_meta( $post->ID,\'meta_box_musicreleases_albumsize\',true ); ?>
<?php if($album_size == \'album_small\'): ?>
<div class="release_small">
<a href="<?php echo esc_url($album_url); ?>" title="<?php the_title_attribute(); ?>" target="_blank"></a>
<?php if($url_type = get_post_meta($post->ID, \'meta_box_musicreleases_linktype\', true)): ?>
<div class="<?php echo ($url_type); ?>"></div>
<?php else: ?>
<?php endif; ?>
<?php the_post_thumbnail(\'music-release-img-sm\'); ?>
</div>
<?php elseif($album_size == \'album_large\'): ?>
<div class="release_large">
<a href="<?php echo esc_url($album_url); ?>" title="<?php the_title_attribute(); ?>" target="_blank"></a>
<?php if($url_type = get_post_meta($post->ID, \'meta_box_musicreleases_linktype\', true)): ?>
<div class="<?php echo ($url_type); ?>"></div>
<?php else: ?>
<?php endif; ?>
<?php the_post_thumbnail(\'music-release-img-lrg\'); ?>
</div>
<?php else: ?>
<!-- no radio button selected -->
<div class="release_large">
<a href="<?php echo esc_url($album_url); ?>" title="<?php the_title_attribute(); ?>" target="_blank"></a>
<?php if($url_type = get_post_meta($post->ID, \'meta_box_musicreleases_linktype\', true)): ?>
<div class="<?php echo ($url_type); ?>"></div>
<?php else: ?>
<?php endif; ?>
<?php the_post_thumbnail(\'music-release-img-lrg\'); ?>
</div>
<?php endif; ?>
<?php else: ?>
<?php $album_size = get_post_meta( $post->ID,\'meta_box_musicreleases_albumsize\',true ); ?>
<?php if($album_size == \'album_small\'): ?>
<div class="release_small">
<?php if($url_type = get_post_meta($post->ID, \'meta_box_musicreleases_linktype\', true)): ?>
<div class="<?php echo ($url_type); ?>"></div>
<?php else: ?>
<?php endif; ?>
<?php the_post_thumbnail(\'music-release-img-sm\'); ?>
</div>
<?php elseif($album_size == \'album_large\'): ?>
<div class="release_large">
<?php if($url_type = get_post_meta($post->ID, \'meta_box_musicreleases_linktype\', true)): ?>
<div class="<?php echo ($url_type); ?>"></div>
<?php else: ?>
<?php endif; ?>
<?php the_post_thumbnail(\'music-release-img-lrg\'); ?>
</div>
<?php else: ?>
<!-- no radio button selected -->
<div class="release_large">
<?php if($url_type = get_post_meta($post->ID, \'meta_box_musicreleases_linktype\', true)): ?>
<div class="<?php echo ($url_type); ?>"></div>
<?php else: ?>
<?php endif; ?>
<?php the_post_thumbnail(\'music-release-img-lrg\'); ?>
</div>
<?php endif; ?>
<?php endif; ?>
<?php endwhile; else: // NO POSTS // ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>