如何在前端检查特定的元框值并相应地输出

时间:2012-09-15 作者:Mr.Brown

我有一个自定义的元框,有两个不同的单选按钮可以选择。一个用于以“大”格式显示内容,另一个用于在前端以“小”格式显示内容。

我已经知道如何检索元框的数据并将其输出,但不知道如何“检查”选择了哪些特定数据,以及如何根据选择的值进行输出。(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>

1 个回复
最合适的回答,由SO网友:Adam 整理而成

这是你需要的吗?

$size = get_post_meta( $post->ID,\'meta_box_musicreleases_featrelease\',true );

if ($size == "display_lrg_release") {

    //do your large thing here

} elseif ($size == "display_sm_release") {

    //do your small thing here

} else {

    //do something when no radio is checked

}
。。。我想是的。

结束

相关推荐

<form> inside a metabox

我正在尝试编写一个插件,允许用户通过Vimeo上载API将视频上载到Vimeo。为了避免试图将视频上传到可能共享的托管帐户,然后再从那里上传到Vimeo,我正试图通过其POST可用性直接上传到Vimeo-https://developer.vimeo.com/apis/advanced/upload#post为此,我必须在一个称为“Vimeo”的自定义帖子类型的元框中放置一个表单,但wordPress正在从返回的HTML源中过滤标签及其属性。有没有办法防止这种情况发生,或者我采取了错误的方法?我相信我可能