Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a little problem with my Metabox.

I explain, I created in below each posts a sharing box and author box, I create a Metabox, it works, but I want hide the two blocks when I click the checkbox of the Metabox instead of show he blocks, it's possible?

Here is the code for my functions.php file for my Metabox:

add_action( 'add_meta_boxes', 'ks_meta_box' );

function ks_meta_box() {
    add_meta_box( 'ks-meta-box', __('Generals Options'), 'ks_meta_box_callback', 'post', 'side', 'default' );
}

function ks_meta_box_callback( $post ) {
  global $post;
  $values = get_post_custom( $post->ID );
  $check = isset( $values['full_width_post'] ) ? esc_attr( $values['full_width_post'][0] ) : '';
  wp_nonce_field( 'ks_meta_box_nonce', 'meta_box_nonce' );
  ?>

  <p>
    <label><input type="checkbox" name="full_width_post" id="full_width_post" <?php checked( $check, 'on' ); ?> /> <?php _e( 'Full-Width Post.' ) ?></label><br />
    <label><input type="checkbox" name="share_buttons" value="open" <?php checked($post->share_buttons, 'open'); ?> /> <?php _e( 'Show Share Buttons.' ) ?></label><br />
    <label><input type="checkbox" name="author_box" value="open" <?php checked($post->author_box, 'open'); ?> /> <?php _e( 'Show Author Information.' ) ?></label>
  </p>

  <?php
}

add_action( 'save_post', 'wpsb_meta_box_save' );
function wpsb_meta_box_save( $post_id ) {

  if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return $post->ID;
  }

  if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'ks_meta_box_nonce' ) ) return;

  if( !current_user_can( 'edit_post' ) ) return;

  $chk = ( isset( $_POST['full_width_post'] ) && $_POST['full_width_post'] ) ? 'on' : 'off';

  update_post_meta( $post_id, 'full_width_post', $chk );
  update_post_meta( $post_id, "share_buttons", $_POST["share_buttons"]);
  update_post_meta( $post_id, "author_box", $_POST["author_box"]);
}

And the code for my sharing box and author box in "single.php" file:

<?php if ( get_post_meta( $post->ID, 'share_buttons', true ) ) : ?>
<?php get_template_part('includes/post-share','single'); ?>
<?php endif ?>

<?php if ( get_post_meta( $post->ID, 'author_box', true ) ) : ?>
<?php ks_author_box() ?>
<?php endif ?>

Thank you.

I hope you can help me, sorry if you misunderstand me but I am French and I use goole translate.

share|improve this question
    
What is var_dump(get_post_meta( $post->ID, 'author_box', true ));? –  s_ha_dum Jan 30 at 3:50
    
This is to show/ hide the author box in each post with the meta-box, it works, but I want to know if this is possible when I press the checkbox to hide my metabox not display the author box? Thank you. –  nick Jan 31 at 13:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.