【发布时间】:2020-02-15 21:36:47
【问题描述】:
我在以下方面解决了同样的问题:
我有一个带有多个复选框元框的 cpt (clinica)。
我创建了元框:
add_meta_box('custom-meta-box', 'Music', 'music_meta_box', 'clinica', 'advanced', 'core');
使用了建议的代码,但出现错误:
警告:in_array() 期望参数 2 是数组,布尔值在 C:\wamp\www\dwa\wp-content\plugins\network-cliniche\network-cliniche.php 在第 86 行
我做错了什么?
我添加了 $callback_args 但我仍然收到相同的错误消息。
这是我的代码:
add_action( 'init', 'network_cliniche_register_post_type' );
function network_cliniche_register_post_type() {
$cliniche_args = array(
'public' => true,
'query_var' => 'clinica',
'rewrite' => array(
'slug' => 'clinica',
'with_front' => false
),
'description' => 'Elenco cliniche appartenti al network',
'has_archive' => 'true',
'capability_type' => 'post',
'taxonomies' => array( 'clinica_tax' ),
'menu_icon' => 'dashicons-networking',
'supports' => array('title', 'editor', 'thumbnail'),
'labels' => array(
'name' => 'Cliniche',
'singular_name' => 'Clinica',
'all_items' => 'Tutte le cliniche',
'add_new' => 'Aggiungi clinica ',
'add_new_item' => 'Aggiungi clinica',
'edit_item' => 'Modifica clinica',
'new_item' => 'Nuova clinica',
'view_item' => 'Visualizza clinica',
'search_items' => 'Cerca cliniche',
'not_found' => 'Nessuna clinica registrata',
'not_found_in_trash' => 'Non sono presenti cliniche nel cestino'
),
'register_meta_box_cb' => 'add_clinica_metaboxes'
);
register_post_type( 'clinica', $cliniche_args );
function add_clinica_metaboxes() {
add_meta_box('dettagli-clinica', 'Dettagli clinica', 'dettagli_clinica', 'clinica', 'advanced', 'core');
add_meta_box('custom-meta-box', 'Music clinica', 'music_meta_box', 'clinica', 'advanced', 'core', $callback_args);
}
}
元框来自:How to save multiple checkbox on update post meta in wordpress?
function music_meta_box()
{
global $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="huge" <?php echo (in_array('huge', $custom_meta)) ? 'checked="checked"' : ''; ?> />Huge
<br>
<input type="checkbox" name="custom-meta-box[]" value="house" <?php echo (in_array('house', $custom_meta)) ? 'checked="checked"' : ''; ?> />House
<br>
<input type="checkbox" name="custom-meta-box[]" value="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);
}
}
}
【问题讨论】:
-
对于初学者来说,添加一些我们可以看到您编写的代码会有很大帮助;)
-
你错过了
$callback_args。 -
我应该放什么来代替 $callback_args?
标签: wordpress checkbox custom-post-type