Php WoodPress管理面板中的WooCommerce类别循环
我试图使用设置API在WordPress的管理面板中创建一个通过WooCommerce产品类别的循环,但以下代码输出是: 注意:中的数组到字符串转换 C:\xampp\htdocs\wordpress2\wp includes\general-template.php在线 3550 这是与general-template.php中的错误相关的函数Php WoodPress管理面板中的WooCommerce类别循环,php,arrays,wordpress,categories,setting,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. * * C
/**
* 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((字符串)$helper==(字符串)$current)
我读过很多类似的问题,但我找不到错误,我知道循环数组有问题。我尝试了var\u 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
将输出多个值。另一个问题是name checkbox参数$options[off\u-woo\u checkbox\u field\u 2]
是多维的,因此应该在other中添加[]
,以存储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>
value=''>
请添加var\u dump($terms)
foreach之前,并与我们共享输出。好的,我已经添加了变量信息。谢谢,请看一下将其应用到代码中的步骤。我想这会解决你的问题。如果没有,请让我知道谢谢,我已经尝试了你建议的代码,但我仍然有相同的错误。可能是我在自定义循环中出错,或者使用了get\u categories()
而不是其他函数。我还在想一个解决办法。你能编辑你的问题,并将$terms
的实际内容添加到你的帖子中吗?我认为您的问题在于$term->name
。第3550行是哪一行?