dougua9328 2018-07-16 23:35
浏览 50

WooCommerce - 获取产品子类别

In WooCommerce, I'm trying to get just the sub-categories of a specific category I've created, called collections and display them in a WP_Query however I can't seem to get just the categories it's marked to show in. I can successfully return all of the categories, just not the specific category it's set to show in.

What I've tried

I've tried using get_terms in conjunction with product_cat taxonomy to no avail and get_categories too. I get all of the categories returned, rather than just the ones the product are marked to show in. I've tried pretty much every other method of getting the categories but still to no avail.

The WP_Query

   <ul class="product-slider">
    <?php

    $args = array(
        'post_type'             => 'product',
        'post_status'           => 'publish',
        'product_cat'              => 'sustainable',
        'ignore_sticky_posts'   => 1,
        'posts_per_page'        => '12'
    );

    $sustain = new WP_Query($args);

    if ($sustain->have_posts()) : while ($sustain->have_posts()) : $sustain->the_post();

        $image = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), 'product-slider-img' );

        ?>

        <div class="single-product">
            <div class="test">
                <div class="product__image">
                    <img src="<?php echo $image[0]; ?>">
                </div>
                <div class="product__meta">
                    <div class="product__name">
                        <?php

                        echo wc_get_product_category_list($product->get_id()) ?>


                        ?>
                    </div>
                    <div class="product__subtitle">
                        <?php echo $product->name; ?>
                    </div>
                    <div class="product__price">
                        £<?php echo $product->price; ?>
                    </div>
                </div>
                <a class="product__link" href="<?php the_permalink(); ?>"></a>
            </div>
        </div>

    <?php

    endwhile; endif;

    wp_reset_query();

    ?>
</ul>
  • 写回答

1条回答 默认 最新

  • dongsong8932 2018-07-17 11:16
    关注

    product_cat is a custom taxonomy, and you can't just replace the normal category parameter with the name of the custom taxonomy. Have a look at the Codex and you'll see that there is a whole tax_query parameter for this use.

    In your example, you should replace 'product_cat' => 'sustainable', with something like this:

    'tax_query' => array(
            array(
                'taxonomy' => 'product_cat',
            ),
        ),
    

    Note: this is the theory, I can't provide a tested example as I'm not at my desk right now.

    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据