1

I have a custom meta box with two different radio buttons that can be selected. One is for displaying the content in a "large' format, the other is for displaying content in a "small" format on the front end.

I already know how to retrieve a meta box's data and output it, but not how to "check" what specific data has been selected, and how to that output according to what value has been selected. (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)

Here is what i have for my meta box (UPDATED):

<?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'] );
}
?>

...As you can see, the meta box allows the user to select whether the album is to be displayed as a LARGE or SMALL release.

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 Answer 1

2

Is this what you need?

$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

}

...I think so.

6
  • Gonna try this in a bit, but it looks like exactly what Im after. Thanks for the quick reply!...Will report back shortly :)
    – Mr.Brown
    Commented Sep 16, 2012 at 0:07
  • So I changed a few things around to more appropriately fit my template and updated the example above to show my changes...Additionally though, I was unable to get this to work so my guess is I missed something because in theory your example makes sense.
    – Mr.Brown
    Commented Sep 16, 2012 at 18:11
  • ...What happens is nothing gets outputted now, so I'm assuming that the way my if checks are scripted are causing the issue however Im not sure what to change?
    – Mr.Brown
    Commented Sep 16, 2012 at 18:16
  • Scratch that, got it to work! Thanks again for pointing me in the right direction!
    – Mr.Brown
    Commented Sep 16, 2012 at 18:49
  • 1
    Ive now updated my example one last time to reflect the working solution for anyone else that might find this helpful.
    – Mr.Brown
    Commented Sep 18, 2012 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.