简体   繁体   English

WordPress获取自定义元框复选框

[英]Wordpress get custom meta box checkbox checked

I have a wordpress custom post type with a custom meta box. 我有一个带有自定义元框的wordpress自定义帖子类型。 With the meta box I can save 3 checkboxes. 使用meta框,我可以保存3个复选框。 But how do I get the data from those checkboxes in my theme? 但是,如何从主题中的这些复选框中获取数据?

This is my function 这是我的职责

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' );

EDIT: I fixed it with this: 编辑:我用此修复它:

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

I fixed it with this: 我用这个修复它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM