admin管理员组文章数量:1287633
How can I display a custom post type of products with a custom taxonomy (product category) in a list, in hierarchical order?
I'd like to get them in this order:
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
Unfortunately I can't wrap my head around the second level (childcategory 1 & 2). I'm using this code to display category and product:
$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;
}
}
I need help figuring out the nested levels of this list.
I'm editing this to try to clarify: thanks so far for the help guys. and sorry for being a noob in posting...
What i'm trying to do is list products that are part of a subcategory, these subcategories can then be part of a big family (Maincategory). I'm trying to group subcategories under a Main category.
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)
How can I display a custom post type of products with a custom taxonomy (product category) in a list, in hierarchical order?
I'd like to get them in this order:
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
Unfortunately I can't wrap my head around the second level (childcategory 1 & 2). I'm using this code to display category and product:
$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;
}
}
I need help figuring out the nested levels of this list.
I'm editing this to try to clarify: thanks so far for the help guys. and sorry for being a noob in posting...
What i'm trying to do is list products that are part of a subcategory, these subcategories can then be part of a big family (Maincategory). I'm trying to group subcategories under a Main category.
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)
Share
Improve this question
edited Oct 22, 2015 at 20:50
tarpier
asked Oct 21, 2015 at 21:44
tarpiertarpier
151 silver badge4 bronze badges
3
|
1 Answer
Reset to default 2Try something like this:
<?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;
wp_reset_postdata();
endif;
}
}
Essentially what you're doing is:
- Getting all parent terms;
- Looping through the parent terms and getting the children of each term;
- Looping through the children terms and getting posts associated with the child.
本文标签: Display custom post type in hierarchical order with getterms
版权声明:本文标题:Display custom post type in hierarchical order with get_terms 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741298009a2370928.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp_reset_query
, instead callwp_reset_postdata
inside the if statement at the very end of your loop – Tom J Nowell ♦ Commented Oct 22, 2015 at 0:40