Категория продукта в полях повторителя - PullRequest
0 голосов
/ 23 февраля 2019

Я пытаюсь отобразить определенные категории продуктов в поле повторителя 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 голосов
/ 25 февраля 2019

Это сработало для меня:

<?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>
...