我使用的代码显示最完整的产品标签的类别页面从100.
The code displays the product tags dynamically with refinements for selection, but an error appears in the line foreach ($tags as $tag)
:
"Warning: foreach() argument must be of type 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>';
}
}