0

I have a custom meta box in my page admin with 5 checkboxes. If one or more of the checkboxes are checked, I want certain content to display on the page.

I have the meta box checkboxes displaying and saving properly in the admin, but what do I need to put in my template file to show content based on whether or not the checkboxes are checked from the admin?

For example, if Web Development, Logo Design and Print Design are checked in the admin, then I want the page to show this:

  • Web Development
  • Logo Design
  • Print Design

I have the following in my functions.php file:

add_action( 'add_meta_boxes', 'add_custom_box' );

function add_custom_box( $post ) {
    add_meta_box(
        'Meta Box', // ID, should be a string.
        'Services', // Meta Box Title.
        'services_meta_box', // Your call back function, this is where your form field will go.
        'page', // The post type you want this to show up on, can be post, page, or custom post type.
        'normal', // The placement of your meta box, can be normal or side.
        'core' // The priority in which this will be displayed.
    );
}

function services_meta_box($post) {
wp_nonce_field( 'my_awesome_nonce', 'awesome_nonce' );    
$checkboxMeta = get_post_meta( $post->ID );
?>

<input type="checkbox" name="ui-ux-design" id="ui-ux-design" value="yes" <?php if ( isset ( $checkboxMeta['ui-ux-design'] ) ) checked( $checkboxMeta['ui-ux-design'][0], 'yes' ); ?> />UI/UX Design<br />
<input type="checkbox" name="web-development" id="web-development" value="yes" <?php if ( isset ( $checkboxMeta['web-development'] ) ) checked( $checkboxMeta['web-development'][0], 'yes' ); ?> />Web Development<br />
<input type="checkbox" name="logo-design" id="logo-design" value="yes" <?php if ( isset ( $checkboxMeta['logo-design'] ) ) checked( $checkboxMeta['logo-design'][0], 'yes' ); ?> />Logo Design<br />
<input type="checkbox" name="branding" id="branding" value="yes" <?php if ( isset ( $checkboxMeta['branding'] ) ) checked( $checkboxMeta['branding'][0], 'yes' ); ?> />Branding<br />
<input type="checkbox" name="print-design" id="print-design" value="yes" <?php if ( isset ( $checkboxMeta['print-design'] ) ) checked( $checkboxMeta['print-design'][0], 'yes' ); ?> />Print Design<br />

<?php }

add_action( 'save_post', 'save_services_checkboxes' );
function save_services_checkboxes( $post_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;
    if ( ( isset ( $_POST['my_awesome_nonce'] ) ) && ( ! wp_verify_nonce( $_POST['my_awesome_nonce'], plugin_basename( __FILE__ ) ) ) )
        return;
    if ( ( isset ( $_POST['post_type'] ) ) && ( 'page' == $_POST['post_type'] )  ) {
        if ( ! current_user_can( 'edit_page', $post_id ) ) {
            return;
        }    
    } else {
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    }

    //saves ui-ux-design's value
    if( isset( $_POST[ 'ui-ux-design' ] ) ) {
        update_post_meta( $post_id, 'ui-ux-design', 'yes' );
    } else {
        update_post_meta( $post_id, 'ui-ux-design', 'no' );
    }

    //saves web-development's value
    if( isset( $_POST[ 'web-development' ] ) ) {
        update_post_meta( $post_id, 'web-development', 'yes' );
    } else {
        update_post_meta( $post_id, 'web-development', 'no' );
    }

    //saves logo-design's value
    if( isset( $_POST[ 'logo-design' ] ) ) {
        update_post_meta( $post_id, 'logo-design', 'yes' );
    } else {
        update_post_meta( $post_id, 'logo-design', 'no' );
    }  
    //saves branding's value
    if( isset( $_POST[ 'branding' ] ) ) {
        update_post_meta( $post_id, 'branding', 'yes' );
    } else {
        update_post_meta( $post_id, 'branding', 'no' );
    }  
    //saves print design's value
    if( isset( $_POST[ 'print-design' ] ) ) {
        update_post_meta( $post_id, 'print-design', 'yes' );
    } else {
        update_post_meta( $post_id, 'print-design', 'no' );
    }  
}
0

Use get_post_meta to fetch each value and compare against 'yes'. E.g.

if(get_post_meta($post->ID, 'ui-ux-design', true) == 'yes') {
  //show relevant content
}
0
if ( 'yes' == get_post_meta( get_the_ID(), 'ui-ux-design' ) ) :
    // Display your content
endif;

Codex: get_post_meta()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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