Вывод списка дочерних категорий и товара находящихся в этих категориях
Доброго время провождения!
Задача вывести список дочерних категорий и товары которые находятся в этих категориях на текущем уровне вложенности.
Имеется такой код который выводит все категории магазина игнорируя активную категорию.
$args = array(
'number' => $number,
'orderby' => 'title',
'order' => 'ASC',
'hide_empty' => $hide_empty,
'include' => $ids,
);
$product_categories = get_terms( 'product_cat', $args );
$count = count($product_categories);
if ( $count > 0 ){
foreach ( $product_categories as $product_category ) {
echo '<h4><a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</a></h4>';
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
// 'terms' => 'white-wines'
'terms' => $product_category->slug
)
),
'post_type' => 'product',
'orderby' => 'title,'
);
$products = new WP_Query( $args );
echo "<ul>";
while ( $products->have_posts() ) {
$products->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php
}
echo "</ul>";
}
}
?>
Есть понимание того что надо получить ID активной категории и получать нужный мне список, но мое знание php недостаточно что бы это реализовать. Накопал такой код, осталось понять как его применить правильно в коде описанном выше.
$current_term = get_queried_object();
$terms = get_terms( [
'taxonomy' => [ 'product_cat' ],
'parent' => $current_term->term_id,
] );
var_dump( $terms );
Надеюсь на ваши подсказки, в результате должна получиться примерно следующая структура:
Магазин > Продукты (Активная категория)
Фрукты (Дочерняя категория)
- Банан (Товар)
- Яблоко
- Груша
Овощи
- Картофель
- Лук
- Перец
Ответы (2 шт):
Попробуйте так
<?php
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'hide_empty' => false,
'parent' => 0
);
$product_categories = get_terms( 'product_cat', $args );
$count = count($product_categories);
if ( $count > 0 ){
foreach ( $product_categories as $product_category ) {
echo '<h4><a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</a></h4>';
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'parent' => $product_category->term_id,
'orderby' => 'title',
'order' => 'ASC'
);
$subcats = get_terms($args);
foreach ($subcats as $subcat ) :
echo ' - <h5><a href="' . get_term_link( $subcat ) . '">' . $subcat->name . '</a></h5>';
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $subcat->term_id
)
),
'post_type' => 'product',
'orderby' => 'title',
'post_status' => 'publish'
);
$products = new WP_Query( $args );
echo "<ul>";
while ( $products->have_posts() ) {
$products->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php
}
wp_reset_postdata();
echo "</ul>";
endforeach;
}
}
?>
С помощью get_queried_object получилось получить массив объектов текущей категории.
В аргументах получаем ID с помощью 'parent' => $current_term->term_id,
$current_term = get_queried_object();
$args = array(
'number' => $number,
'orderby' => 'title',
'order' => 'ASC',
'hide_empty' => $hide_empty,
'include' => $ids,
'parent' => $current_term->term_id,
);
$product_categories = get_terms( 'product_cat', $args );
$count = count($product_categories);
if ( $count > 0 ){
foreach ( $product_categories as $product_category ) {
echo '<h4><a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</a></h4>';
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
// 'terms' => 'white-wines'
'terms' => $product_category->slug
)
),
'post_type' => 'product',
'orderby' => 'title,'
);
$products = new WP_Query( $args );
echo "<ul>";
while ( $products->have_posts() ) {
$products->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php
}
echo "</ul>";
}
}