Для вывода всех категорий и подкатегорий мы будем использовать такой код:
Код
<div>
РАЗРАБОТКА ФОРМИРОВАНИЯ КАТЕГОРИЙ И ПОДКАТЕГОРИЙ ИЗ АДМИНКИ
<?php
$taxonomy = 'product_cat';
//$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if(($cat->category_parent == 0) && ($cat->term_id <> 15)) {
$category_id = $cat->term_id;
echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo '<br /><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a>';
//echo $sub_category->name ;
}
}
}
}
?>
</div>
И в этом коде мы не выводим категорию "misk", мы ее отбрасываем условием.
Тут нестандартный случай, потому что нам нужно было вывести категории и подкатегории с левой стороны сайта в виде раскрывающегося аккордеона.
Пример
<div class="container-main__menu">
<p class="container-main__menu-maintext">КАТЕГОРИИ</p>
<div class="accordion accordion-flush" id="accordionFlushExample">
<?php
$taxonomy = 'product_cat';
//$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if(($cat->category_parent == 0) && ($cat->term_id <> 15)) {
$category_id = $cat->term_id;
?>
<div class="accordion-item">
<div class="accordion-header" id="flush-heading<? echo $cat->term_id; ?>">
<a class="container-main__menu-mainitem" href="<? echo get_term_link($cat->slug, 'product_cat'); ?>"><? echo $cat->name; ?></a>
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapse<? echo $cat->term_id; ?>" aria-expanded="false" aria-controls="flush-collapseOne">
</button>
</div>
<div id="flush-collapse<? echo $cat->term_id; ?>" class="accordion-collapse collapse" aria-labelledby="flush-heading<? echo $cat->term_id; ?>" data-bs-parent="#accordionFlushExample">
<div class="accordion-body">
<?php
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
?>
<a class="container-main__menu-item" href="<? echo get_term_link($sub_category->slug, 'product_cat'); ?>"><i class="fa-solid fa-chevron-right"></i> <? echo $sub_category->name; ?></a>
<?
}
}
?>
</div>
</div>
</div>
<?
}
}
?>
</div>
<div class="container-main__banner">
<a class="container-main__banner-link" href="">
<img class="container-main__banner-link-img" src="/wp-content/themes/russianwool/assets/images/banner.jpg" alt="">
</a>
</div>
</div>