Wordpress获取自定义元框复选框

时间:2014-10-10 13:32:36

标签: php wordpress meta-boxes

我有一个带有自定义元框的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";
}

1 个答案:

答案 0 :(得分:1)

我用它修理了它:

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