カスタム分類法(商品カテゴリ)を持つ商品のカスタム投稿タイプを階層順に一覧表示する方法を教えてください。
私はこの順番でそれらを手に入れたいです。
h3.parentcategory 1
-h4.childcategory 1
-li.product of childcategory 1
-li.product of childcategory 1
-h4.childcategory 2
-li.product of childcategory 2
-li.product of childcategory 2
h3.parentcategory 2
-h4.childcategory 1 of parent 2
残念ながら、私は頭を2番目のレベル(childcategory 1&2)に巻き付けることができません。カテゴリと商品を表示するためにこのコードを使用しています。
$custom_terms = get_terms( 'productcategory' );
foreach ( $custom_terms as $custom_term ) {
wp_reset_query();
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'productcategories',
'field' => 'slug',
'terms' => $custom_term->slug,
'orderby' => 'term_group',
),
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
echo '<h3>' . $custom_term->name . '</h2>';
while ( $loop->have_posts() ) : $loop->the_post();
get_the_title();
endwhile;
}
}
このリストのネストされたレベルを把握するのに手助けが必要です。
明確にするためにこれを編集しています。これまでのところ、皆さん、ありがとう。そして、投稿に慣れていないのが残念...
私がやろうとしているのは、サブカテゴリの一部である商品を一覧表示することです。これらのサブカテゴリは、大家族(Maincategory)の一部になることができます。サブカテゴリをメインカテゴリの下にグループ化しようとしています。
vegetables (Main)
-tomatoes (sub)
-red tomatoe sweet (product)
-pink tomatoe sour (product)
-cucumber (sub)
-some kind of cucumber(product)
fruits (Main)
-Apple(sub)
-sweet Apple (product)
-Golden delicious (product)
このようなことを試してください:
<?php
$parent_terms = get_terms(
'name_of_your_taxonomy',
array(
'parent' => 0,
)
);
foreach( $parent_terms as $parent_term ) {
$child_terms = get_terms(
'name_of_your_taxonomy'
array(
'child_of' => $parent_term->term_id,
)
);
foreach( $child_terms as $child_term ) {
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'name_of_your_taxonomy',
'field' => 'slug',
'terms' => $child_term->slug,
),
),
);
$loop = new WP_Query($args);
if( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
echo '<h3>' . $parent_term->name . '</h3>';
echo '<h4>' . $parent_term->name . '</h4>';
echo get_the_title();
endwhile;
endif;
}
}
wp_reset_query();
基本的にあなたがしていることは、