如何运行循环以从Wordpress中的多个复选框中获取数据?

时间:2018-12-28 10:08:55

标签: wordpress meta-boxes

我正在尝试从复选框中获取数据,但这给了我错误:

"Array to string conversion in C:\xampp\htdocs\blogger\wp-content\themes\blogger\single-book.php on line 16"

我尝试了以下代码:

function.php

    $books->add_field(array(
            'id'         => 'select',
            'name'       => 'This is a checkboxes',
       //   'desc'       => 'How to Train Your Dragon Story',
            'type'    => 'multicheck',
            'options' => array(
            'check1' => 'Check One',
            'check2' => 'Check Two',
            'check3' => 'Check Three',
        ),

        ));

index.php

     <?php echo get_post_meta(get_the_id(),'select',true);

1 个答案:

答案 0 :(得分:1)

似乎您正在使用CMB2插件,您的代码
<?php echo get_post_meta(get_the_id(),'select',true); ?>
将返回带有值的数组。仅检查值会出现。在您的代码中get_the_ID()被错误地称为 id ,应在大写字母中。

    $vals= get_post_meta(get_the_ID(),'select',true); 
    foreach ($vals as $key => $v) {
        echo $v;
    }

希望这对您有用,