Answer the question
In order to leave comments, you need to log in
How to list Woocommerce categories and subcategories?
Good afternoon, tell me how can I display a list of woocommerce categories in this form?
<li>Категория
<ul>
<li>Подкатегория </li>
</ul>
</li>
Answer the question
In order to leave comments, you need to log in
I don’t know how relevant this code is, but I did it like this
$category_id = get_query_var( 'cat' ); // Get current catgory ID
$category = get_term( $category_id, 'category' ); // Fetch category term object
// get_categories(['']);
// Now, we check if the category has a parent
// If it has, we use that ID
// If it doesn't have a parent, it is a parent category itself and we use its own ID
$parent = $category->parent ? $category->parent : $category_id;
$args = array(
'show_count' => false,
'hide_empty' => false,
'title_li' => '',
'show_option_none' => '',
'echo' => false
);
echo "parent: {$category->parent}";
// echo "category_id: $category_id";
// Show the children of parent category
if ( $category->parent ) {
$args['child_of'] = $category->parent;
$args['exclude'] = $category_id; // Don't display the current category in this list
}
else {
// $args['child_of'] = $category_id;
}
// $args['show_option_all'] = 'Все';
$args['hide_empty'] = true;
// Get the category list
$categories_list = wp_list_categories( $args );
if ( $categories_list ) {
?>
<div class="category-wrapper">
<ul class="child-categories">
<?php echo $categories_list; ?>
</ul>
</div>
<?php
}
There is a Wordpress Walker_category class. It displays a list of categories with ul tags
Take it, change the name and throw out everything superfluous (what you consider superfluous), get a class for displaying the list, in my opinion very convenient
Allows you to separate the code from the layout
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question