2

製品カテゴリに割り当てられた製品を循環するカスタム ループを設定しようとしていますが、機能していないようです。

私のカテゴリー設定:

工場直送 - FD1 - FD2 - FD3

ID が 84 の Factory Direct の ANY CHILDREN CATEGORIES に該当する製品をループに表示したいと考えています。

私のテンプレートでこれをコーディングしようとする試み:

<ul class="products factoryloop">
    <?php
        $args = array(
            'post_type' => 'product',
            'posts_per_page' => 12,
            'cat' => 84
            );
        $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-->

ID を 84 から特定のカテゴリ (ID が 24 の FD1 の例) に変更しようとしましたが、まだ機能していません。

アイデア/提案はありますか?

WP_Query で cat 引数を削除すると製品をループしますが、ループを指定できません。

ありがとう!

4

2 に答える 2

1

これは私が query_posts で行った方法です。 WP_Queryで同じことを行う必要があります。

function getCategoryByParent ($id) {
    $args=array(
      'orderby' => 'name',
      'parent' => $id,
      'hide_empty' => false,
      'taxonomy' => 'product_cat',
      'order' => 'ASC',
      );
    $categories=get_categories($args);
    return $categories;
}
$cats = getCategoryByParent(84);
query_posts( array( 'paged' => $paged, 'posts_per_page' => 9, 'post_type' => 'product', 'post_status' => 'publish' ,  'taxonomy' => 'product_cat', 'tax_query' => array( 
            array(
              'taxonomy' => 'product_cat',
              'field' => 'id',
              'terms' => $cats
            ))));

お役に立てば幸いです、Asaf。

于 2013-09-17T23:57:20.153 に答える