How to check for specific meta box value on front end and output accordingly

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)

Read More

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>

Related posts

Leave a Reply

1 comment

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