选中Wordpress获取自定义元框复选框


Wordpress get custom meta box checkbox checked

我有一个带有自定义元框的wordpress自定义帖子类型。有了元框,我可以保存3个复选框。但是我如何从主题中的复选框中获取数据呢?

这是我的功能

function social_services( $post )
{
    // Get post meta value using the key from our save function in the second paramater.
    $custom = get_post_meta($post->ID, '_social_services', true);
    ?>
        <input type="checkbox" id="social_services_soundcloud" name="social_services[]" value="soundcloud" <?php echo (in_array('soundcloud', $custom)) ? 'checked="checked"' : ''; ?>>
        <label for="social_services_soundcloud"></label>Soundcloud<br>
        <input type="checkbox" id="social_services_facebook" name="social_services[]" value="facebook" <?php echo (in_array('facebook', $custom)) ? 'checked="checked"' : ''; ?>>
        <label for="social_services_facebook"></label>Facebook<br>
        <input type="checkbox" id="social_services_twitter" name="social_services[]" value="twitter" <?php echo (in_array('twitter', $custom)) ? 'checked="checked"' : ''; ?>>
        <label for="social_services_twitter"></label>Twitter<br>
    <?php
}
function save_extra_fields(){
  global $post;
    if(isset( $_POST['social_services'] ))
    {
        $custom = $_POST['social_services'];
        $old_meta = get_post_meta($post->ID, '_social_services', true);
        // Update post meta
        if(!empty($old_meta)){
            update_post_meta($post->ID, '_social_services', $custom);
        } else {
            add_post_meta($post->ID, '_social_services', $custom, true);
        }
    }
  // update_post_meta($post->ID, "producers", $_POST["producers"]);
}
add_action( 'save_post', 'save_extra_fields' );

编辑:我用这个修复了它:

if (in_array('soundcloud', get_post_meta($post->ID, '_social_services', true)) == true) {
  // Show the content here
  echo "soundcloud";
}

我用这个修复了它:

if (in_array('soundcloud', get_post_meta($post->ID, '_social_services', true)) == true) {
  // Show the content here
  echo "soundcloud";
}