WordPress管理面板中的WooCommerce类别循环

时间:2016-12-27 20:38:07

标签: php arrays wordpress categories setting

我尝试使用设置API在WordPress的管理面板中创建WooCommerce产品类别的循环,但以下代码输出为:

  

注意:数组转换为字符串   C:\ xampp \ htdocs \ wordpress2 \ wp-includes \ general-template.php在线   3550

这是与general-template.php中的错误相关的函数。

/**
 * Private helper function for checked, selected, and disabled.
 *
 * Compares the first two arguments and if identical marks as $type
 *
 * @since 2.8.0
 * @access private
 *
 * @param mixed  $helper  One of the values to compare
 * @param mixed  $current (true) The other value to compare if not just true
 * @param bool   $echo    Whether to echo or just return the string
 * @param string $type    The type of checked|selected|disabled we are doing
 * @return string html attribute or empty string
 */
function __checked_selected_helper( $helper, $current, $echo, $type ) {
    if ( (string) $helper === (string) $current )
        $result = " $type='$type'";
    else
        $result = '';

    if ( $echo )
        echo $result;

    return $result;
}

第3550行就是这一行:if ( (string) $helper === (string) $current )

我已经阅读了许多类似的问题,但我无法找到错误,我知道循环数组存在问题。我已尝试var_dump()变量,似乎此代码将输出2个数组,但我无法隔离我想要使用的值["name"]。 这是变量$terms

的输出
 array(2) { [0]=> object(WP_Term)#9234 (16) { ["term_id"]=> int(8) ["name"]=> string(6) "mobile" ["slug"]=> string(6) "mobile" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(8) ["taxonomy"]=> string(11) "product_cat" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(4) ["filter"]=> string(3) "raw" ["cat_ID"]=> int(8) ["category_count"]=> int(4) ["category_description"]=> string(0) "" ["cat_name"]=> string(6) "mobile" ["category_nicename"]=> string(6) "mobile" ["category_parent"]=> int(0) } [1]=> object(WP_Term)#9266 (16) { ["term_id"]=> int(9) ["name"]=> string(4) "vino" ["slug"]=> string(4) "vino" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(9) ["taxonomy"]=> string(11) "product_cat" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(1) ["filter"]=> string(3) "raw" ["cat_ID"]=> int(9) ["category_count"]=> int(1) ["category_description"]=> string(0) "" ["cat_name"]=> string(4) "vino" ["category_nicename"]=> string(4) "vino" ["category_parent"]=> int(0) } }

感谢您的帮助。

<?php
function offwoo_checkbox_field_2_render() {
    $options = get_option( 'offwoo_settings' );
    global $woocommerce;

    $terms = get_categories( array(
        'taxonomy' => 'product_cat',
        'orderby'   =>'name',
        'parent'  => 0 
    ));

   var_dump($terms);

    foreach ($terms as $term) {
    ?>
    <input type='checkbox' name='offwoo_settings[offwoo_checkbox_field_2]' <?php if(isset($options['offwoo_checkbox_field_2'])) { checked( $options['offwoo_checkbox_field_2'], 1 ); } ?> value='<?php echo $term->name; ?>'>
    <label><?php echo $term->name; ?></label>
    <?php   
    }
}
?>

1 个答案:

答案 0 :(得分:0)

我也找到了这个问题的解决方案,这要归功于另外一个:

https://wordpress.stackexchange.com/questions/183007/checked-function-on-a-multidimensional-array

在这种情况下,正如上面评论中正确地提到的那样,问题是checked()函数设置错误,并且只能使用一个参数而不是数组,因此in_array()应该在这种情况下添加。在这种情况下,$term->name将输出多个值。另一个问题是名称复选框参数$options[off_woo_checkbox_field_2]是多维的,因此应在其他位置添加[]以存储WooCommerce循环的其他参数创建。

<input type='checkbox' name='offwoo_settings[offwoo_checkbox_field_2][]' 
<?php if(isset($options['offwoo_checkbox_field_2'])) { checked( in_array($term->name, $options['offwoo_checkbox_field_2']), true); }?> value='<?php echo $term->name; ?>'>
<label><?php echo $term->name; ?></label>