我正在尝试使用设置API在WordPress的管理面板中通过WordPress的WooCommerce的产品类别创建一个循环,但是以下代码输出为:
注意:到字符串转换的数组 c: xampp htdocs wordpress2 wp-includes ender-template.php on Line 3550
这是与通用 - 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
}
}
?>
我也找到了这个问题的解决方案:
: 在在这种情况下,如上所述在上面的注释中正确添加的问题是,checked()
函数以错误的方式设置,并且只能采用一个参数而不是一个数组,因此应在这种情况下添加in_array()
。在这种情况下,$term->name
将输出多个值。另一个问题是名称复选框参数$options[off_woo_checkbox_field_2]
是多维的,因此应将[]
添加到其他方面,以存储WooCommerce Loop创建的其他参数。
<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>