Reputation: 269
I want a simple overview of categories and their subcategories. When clicking the parent category only subcategories of that category are shown. But I also want the subcategories to show when in a different subcategory of the same parent. I realise the queried_object_id(); changes when clicking on the subcategory. But how do I let the query know?
My following code:
<ul>
<?php
$parentid = get_queried_object_id();
$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) {
$category_id = $cat->term_id;
//echo $category_id;
echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .' - '. $category_id .'</a></li>';
//echo $parentid;
if($parentid == $category_id) {
//$cat = get_queried_object();
$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) {
echo '<ul>';
foreach($sub_cats as $sub_category) {
//echo $parentid;
echo '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .' - '. $sub_category->term_id .'</a></li>';
//echo $sub_category->name ;
}
echo '</ul>';
}
}
}
}
?>
</ul>
Upvotes: 1
Views: 1031
Reputation: 253784
Why you dont simply use the woocommerce Widget for product categories with Wordpress the_widget()
function, as you will get everything this way:
the_widget( 'WC_Widget_Product_Categories', array( 'hide_empty' => false, 'hierarchical' => true, 'show_children_only' => true ) );
It will display a hierarchical vertical list of linked product categories.
With
the_widget()
function you can with:
- the 2nd parameter argument make changes in the Query
- the 3rd parameter argument make changes in the html output
Upvotes: 2
Reputation: 269
I changed the code based on getting $parentid2 = get_queried_object();
Note: this code is placed in /woocommerce/archive-product.php
and I added an elseif statement with different args.
Maybe a little bit too much but it works
<ul>
<?php
$parentid2 = get_queried_object();
$parentid = get_queried_object_id();
$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) {
$category_id = $cat->term_id;
echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></li>';
if($parentid == $category_id) {
$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) {
echo '<ul>';
foreach($sub_cats as $sub_category) {
//echo $parentid;
echo '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a></li>';
//echo $sub_category->name ;
}
echo '</ul>';
}
}
elseif($parentid2->parent == $category_id) {
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $parentid2->parent,
'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) {
echo '<ul>';
foreach($sub_cats as $sub_category) {
//echo $parentid;
echo '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a></li>';
//echo $sub_category->name ;
}
echo '</ul>';
}
}
}
}
?>
</ul>
Upvotes: 1
Reputation: 31
you can exclude current category/term by
'exclude' => $catID
in the arguments.
Upvotes: -1