I am using the code for displaying the most complete product tags on the category page from here.
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."
I don’t understand what exactly I need to fix and how to fix this warning. Any help will be appreciated.
Here is the code that I am using:
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>';
}
}
2
Answers
In your code, for a
foreach
loop, it is better to replaceget_the_terms()
withwp_get_post_terms()
, as if there are no terms found,wp_get_post_terms()
returns an empty array, which is not the case forget_the_terms()
that returnsfalse
and throws that warning message.Also, your current code can be lightened and optimized. Try the following:
It should not throw anymore this warning message.
WordPress is written in PHP, a polymorphic language, which is common knowledge. The negative (or fail) return is often a boolean or a WP_Error. Check the official documentation for function returns. Therefore, you need to validate your data before executing your loop.
It’s good practice to always validate data before executing any code.