简体   繁体   English

通过类别选择产品,woocommerce

[英]Select products via category, woocommerce

I'm looking to pull the names and prices of all of the products listed under a certain category in WooCommerce. 我正在寻找WooCommerce中某个类别下列出的所有产品的名称和价格。 The category name is Recyclable, and it has an ID of 48 under the wp_terms table. 类别名称是可回收的,并且在wp_terms表下的ID为48。 Looking in the database, under wp_posts I see all of the products, but I don't see any column linking them to a certain category (48, or "Recyclable"). 在数据库中的wp_posts下,我看到了所有产品,但是没有看到任何列将它们链接到某个类别(48,或“可回收”)。

I could pull these directly off the page using regex, but that would be pretty inconvenient and slow. 我可以使用regex直接将其从页面上拉出,但这将非常不便且缓慢。

Does anyone know how these WordPress, or WooCommerce stores these products in they're right categories? 有谁知道这些WordPress或WooCommerce如何将这些产品存储在正确的类别中?

In woocommerce product is a custom post type and product category is a custom taxonomy (product_cat). 在woocommerce中,产品是自定义帖子类型,产品类别是自定义分类法(product_cat)。 You can use wp_query to get the products to a certain category. 您可以使用wp_query将产品归类。

<ul class="products">
    <?php
        $args = array(
            'post_type' => 'product',
            'posts_per_page' => 12,
            'tax_query' => array(
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'id',
                        'terms' => 48
                    )
                )
            );
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : $loop->the_post();
                woocommerce_get_template_part( 'content', 'product' );
            endwhile;
        } else {
            echo __( 'No products found' );
        }
        wp_reset_postdata();
    ?>
</ul><!--/.products-->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM