转发器字段中的产品类别

时间:2019-02-23 20:59:02

标签: php woocommerce repeater advanced-custom-fields

我正在尝试在ACF转发器字段中显示特定的产品类别。 这样可以继续输出所有产品类别。如何在每个转发器字段中输出单个类别?

<?php if( have_rows('product_categories') ): ?>

<ul class="products">

    <?php while( have_rows('product_categories') ): the_row(); ?>

        <?php
        $product_category_ids = get_sub_field('project_category');
        $args = array(
            'post_type' => 'product',
            'posts_per_page' => -1,
            'tax_query' => array(
                'taxonomy' => 'product_cat',
                'terms' => $product_category_ids
            ),
        );

        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : $loop->the_post();
                wc_get_template_part( 'content', 'product' ); ?>

                <?php
            endwhile;
        } else {
            echo __( 'No products found' );
        }
        wp_reset_postdata();
        ?>

    <?php endwhile; ?>

</ul>

1 个答案:

答案 0 :(得分:0)

这对我有用:

<?php if( have_rows('product_categories') ): ?>

<ul class="products">

    <?php while( have_rows('product_categories') ): the_row(); ?>

        <?php
        $product_cats = get_sub_field('product_category');
        $args = array(
            'post_type'             => 'product',
            'post_status'           => 'publish',
            'posts_per_page'        => '12',
            'tax_query'             => array(
                array(
                    'taxonomy'      => 'product_cat',
                    'field' => 'term_id',
                    'terms'         => $product_cats,
                    'operator'      => 'IN',
                    'field'         => 'slug'
                ),
            )
        );
        $loop = new WP_Query($args);
        while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

            <?php wc_get_template_part( 'content', 'product' ); ?>

        <?php endwhile; ?>

        <?php wp_reset_query(); ?>

    <?php endwhile; ?>

</ul>