【问题标题】:Product category in repeater fields转发器字段中的产品类别
【发布时间】:2019-07-17 16:10:03
【问题描述】:

我试图在 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>

【问题讨论】:

  • 您应该在“tax_query”中添加“field”参数
  • 谢谢,还是漏掉了几个参数....大大的叹息

标签: php woocommerce repeater advanced-custom-fields


【解决方案1】:

这对我有用:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    • 1970-01-01
    • 2014-06-21
    相关资源
    最近更新 更多