我使用的代码显示最完整的产品标签的类别页面从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>';
    }
} 

推荐答案

在您的代码中,for a 100 loop,最好用wp_get_post_terms()替换get_the_terms(),就像没有找到任何术语一样,wp_get_post_terms()返回一个空数组,而get_the_terms()则不是这样,它返回false并抛出警告消息.

此外,您当前的代码可以简化和优化.try 以下操作:

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>';
        }
    }
}

它不应该再抛出这条警告消息.

Php相关问答推荐

显示WooCommerce当前产品类别或标签的短代码

Mysqli_stmt::Execute():结果字段元数据中存在过早的EOF

无法在Laravel/Vue停靠容器中启动MySQL

Laravel Nova不使用数组键作为筛选器中的选项值

PHP-准备用于TCP套接字的数据包

按列值将二维数组排序为不超过N个的递增组

如果日期是过go 日期,则隐藏事件发布类型

允许客户定义特定WooCommerce产品的价格

为什么WooCommerce的动作钩子';woocommerce_checkout_update_order_review';包含旧信息?

文件::Files()响应返回空对象数组

ANTLR4-lexer如何消耗更多的 token 并停止现有规则?

无法在 Laravel 中实例化抽象类 ProductAbstract

无法使用 slim 的父级容器

获取 Shopware 6 集合元素的值,PHP

避免在 WooCommerce 中多次触发挂钩函数

Laravel使用另一张表进行关联查询

自定义主键值未正确显示

我正在使用 Baryryvdh/laravel-snappy 从 HTML 导出 PDF 它正在工作但缩小了 pdf 文件

基于其他数组创建多维数组 struct

我想更新表格中的结果并将它们发布到浏览器,但它不起作用