2014-10-10 128 views
0

我有一个WordPress自定义帖子类型与自定义元框。 使用元框我可以保存3个复选框。但是,如何从我的主题中的这些复选框获取数据?Wordpress获取自定义元框复选框选中

这是我的功能

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"; 
} 
+0

正在保存到数据库中的元值精细?您可以单独使用它们:'<?php $ meta_values = get_post_meta($ post_id,$ key,$ single); ?> – rnevius 2014-10-10 13:37:22

+0

这是节省的罚款,我也看到当我切换页面时,复选框仍然被选中。 – 2014-10-10 13:45:26

+0

为什么不能使用保存元键值的'$ custom'变量来编写条件?例如:'if($ custom){// Do Something}' – rnevius 2014-10-10 14:25:03

回答

1

我这个固定:

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