我正在使用代码在类别页面上显示最完整的产品标签,来自这里。
代码动态显示产品标签,并进行了选择细化,但行 foreach ($tags as $tag) 中出现错误: “警告:foreach() 参数必须是 array|object, bool 类型。”
我不太明白我到底需要修复什么以及如何修复这个错误。 任何帮助,谢谢。
function custom_product_tag_cloud_func() {
if (is_shop()) {
$args= array(
'smallest' => 13,
'largest' => 13,
'taxonomy' => 'product_tag',
'unit' => 'px',
'number' => 10,
'format' => 'flat',
'separator' => " ",
'orderby' => 'count',
'order' => 'DESC',
'show_count' => 0,
'number' => 0,
);
echo'<div class="widget_product_tag_cloud"><span class="gamma widget-title"></span><div class="tagcloud"><ul class="wp-tag-cloud">';
wp_tag_cloud($args);
echo '</ul></div></div>';
}
global $wp;
$current_slug = add_query_arg( array(), $wp->request );
if(strpos($current_slug, '/') !== false) {
$newtest = explode('/', $current_slug);
$current_slug = array_pop($newtest);
}
$args = array(
'category' => array( $current_slug ),
'limit' => -1,
);
$products = wc_get_products( $args );
$tags_objects = array();
foreach ($products as $product) {
$tags = get_the_terms( $product->get_id(), 'product_tag' );
foreach ($tags as $tag) {
$slug = $tag->slug;
$cat_slug = add_query_arg( array(), $wp->request );
$tag->link = get_term_link( $tag ); //get_site_url()."/".$cat_slug."?product_tag=".$slug;
array_push($tags_objects,$tag);
}
}
$tags_objects = array_unique($tags_objects, SORT_REGULAR);
$args = array(
'smallest' => 13,
'largest' => 13,
'number' => 10,
'format' => 'flat',
'separator' => " ",
'orderby' => 'count',
'order' => 'DESC',
'unit' => 'px',
'show_count' => 0,
'number' => 0,
);
$tag_cloud = wp_generate_tag_cloud($tags_objects,$args);
if(is_product_category()){
echo'<div class="widget_product_tag_cloud"><span class="gamma widget-title"></span><div class="tagcloud"><ul class="wp-tag-cloud">';
echo $tag_cloud;
echo '</ul></div></div>';
}
}
在您的代码中,对于
foreach
循环,最好将get_the_terms()
替换为wp_get_post_terms()
,就好像没有找到术语一样,wp_get_post_terms()
返回一个空数组,而则不是这样get_the_terms()
返回 false
并抛出该警告消息。
此外,您当前的代码可以得到简化和优化。尝试以下操作:
function custom_product_tag_cloud_func() {
if ( is_shop() ) {
echo'<div class="widget_product_tag_cloud"><span class="gamma widget-title"></span><div class="tagcloud"><ul class="wp-tag-cloud">';
wp_tag_cloud( array(
'smallest' => 13,
'largest' => 13,
'taxonomy' => 'product_tag',
'unit' => 'px',
'number' => 10,
'format' => 'flat',
'separator' => " ",
'orderby' => 'count',
'order' => 'DESC',
'show_count' => 0,
'number' => 0,
) );
echo '</ul></div></div>';
}
elseif ( is_product_category() ) {
global $wp;
$current_slug = add_query_arg( array(), $wp->request );
if(strpos($current_slug, '/') !== false) {
$newtest = explode('/', $current_slug);
$current_slug = array_pop($newtest);
}
$product_ids = wc_get_products( array(
'status' => 'publish',
'category' => array( $current_slug ),
'limit' => -1,
'return' => 'ids', // return only product Ids
) );
$tags_objects = array();
foreach ($product_ids as $product_id) {
$tags = wp_get_post_terms( $product_id, 'product_tag' );
if ( ! empty( $tags ) && ! is_wp_error( $tags ) ) {
foreach ( $tags as $tag ) {
$slug = $tag->slug;
$cat_slug = add_query_arg( array(), $wp->request );
$tag->link = get_term_link( $tag );
array_push($tags_objects, $tag);
}
}
}
if( ! empty($tags_objects) ){
$tags_objects = array_unique($tags_objects, SORT_REGULAR);
echo'<div class="widget_product_tag_cloud"><span class="gamma widget-title"></span><div class="tagcloud"><ul class="wp-tag-cloud">';
echo wp_generate_tag_cloud($tags_objects, array(
'smallest' => 13,
'largest' => 13,
'number' => 10,
'format' => 'flat',
'separator' => " ",
'orderby' => 'count',
'order' => 'DESC',
'unit' => 'px',
'show_count' => 0,
'number' => 0,
) );
echo '</ul></div></div>';
}
}
}
它不应该再抛出此警告消息。