我有多个复选框, 我想保存该复选框并在update_post_meta中对其进行更新, 以便下次用户编辑帖子时, 他会知道已经选中了哪些复选框。我不知道我在哪里弄错了。这是我的代码:
add_action( 'add_meta_boxes', 'add_custom_box' );
function add_custom_box( $post ) {add_meta_box(
'Meta Box', // ID, should be a string
'Music', // Meta Box Title
'music_meta_box', // Your call back function, this is where your form field will go
'post', // The post type you want this to show up on, can be post, page, or custom post type
'side', // The placement of your meta box, can be normal or side
'core' // The priority in which this will be displayed
);
}function music_meta_box( $post ) {// Get post meta value using the key from our save function in the second paramater.
$custom_meta = get_post_meta($post->
ID, 'custom-meta-box', true);
?>
<
input type="checkbox" name="custom-meta-box[]" value="http://www.srcmini.com/huge" <
?php if(isset($_POST['custom-meta-box']['huge'])) echo 'checked="checked"';
?>
/>
Huge <
br>
<
input type="checkbox" name="custom-meta-box[]" value="http://www.srcmini.com/house" <
?php if(isset($_POST['custom-meta-box']['house'])) echo 'checked="checked"';
?>
/>
House <
br>
<
input type="checkbox" name="custom-meta-box[]" value="http://www.srcmini.com/techno" <
?php if(isset($_POST['custom-meta-box']['techno'])) echo 'checked="checked"';
?>
/>
Techno <
br>
<
?php}
add_action( 'save_post', 'save_music_meta_box' );
function save_music_meta_box(){global $post;
// Get our form field
if(isset( $_POST['custom-meta-box'] )) :$custom = $_POST['custom-meta-box'];
// Update post metaforeach($ids as $id){
update_post_meta($id, '_custom-meta-box', $custom[$id]);
wp_set_object_terms( $id, $custom[$id], 'Music' );
}endif;
}
请帮忙
#1试试这个逻辑:
function music_meta_box( $post )
{
// Get post meta value using the key from our save function in the second paramater.
$custom_meta = get_post_meta($post->
ID, '_custom-meta-box', true);
?>
<
input type="checkbox" name="custom-meta-box[]" value="http://www.srcmini.com/huge" <
?php echo (in_array('huge', $custom_meta)) ? 'checked="checked"' : '';
?>
/>
Huge
<
br>
<
input type="checkbox" name="custom-meta-box[]" value="http://www.srcmini.com/house" <
?php echo (in_array('house', $custom_meta)) ? 'checked="checked"' : '';
?>
/>
House
<
br>
<
input type="checkbox" name="custom-meta-box[]" value="http://www.srcmini.com/techno" <
?php echo (in_array('techno', $custom_meta)) ? 'checked="checked"' : '';
?>
/>
Techno<
br>
<
?php
}
add_action( 'save_post', 'save_music_meta_box' );
function save_music_meta_box()
{global $post;
// Get our form field
if(isset( $_POST['custom-meta-box'] ))
{
$custom = $_POST['custom-meta-box'];
$old_meta = get_post_meta($post->
ID, '_custom-meta-box', true);
// Update post meta
if(!empty($old_meta)){
update_post_meta($post->
ID, '_custom-meta-box', $custom);
} else {
add_post_meta($post->
ID, '_custom-meta-box', $custom, true);
}
}
}
【如何在wordpress中更新更新后的元数据上保存多个复选框()】希望对你有帮助。
推荐阅读
- 如何设置分类列表限制()
- 如何从子主题中删除WordPress主题选项
- mysql配置主从配置遇到的坑
- AWS EKS Horizontal Pod Autoscaler(HPA)
- web服务之源码编译安装LAMP(编译安装PHP 解析环境)
- python自动化测试六
- LAMP(ApacheMySQLPHP)
- 运维工程师必备利器|一招实现运维智能化!
- Web服务之Nginx优化与防盗链