Php 选中Wordpress获取自定义元框复选框
我有一个wordpress自定义帖子类型和一个自定义元框。 使用meta框,我可以保存3个复选框。但是我如何从主题中的复选框中获取数据呢 这是我的职责Php 选中Wordpress获取自定义元框复选框,php,wordpress,meta-boxes,Php,Wordpress,Meta Boxes,我有一个wordpress自定义帖子类型和一个自定义元框。 使用meta框,我可以保存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, '_socia
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";
}
元数据保存到数据库中是否正常?您可以使用以下命令单独访问它们:
保存很好,我还看到切换页面时复选框仍处于选中状态。为什么不能使用保存元键值的$custom
变量编写条件?类似于:if($custom){//Do Something}
我用这个来修复它:if(在数组('soundcloud',get_post_meta($post->ID,'u social_services',true))==true){//在这里显示内容回音“soundcloud”}
你可以去掉结尾==true
来简化事情。将您的查找添加为答案,而不是编辑!我会投票的。善于解决问题。