Use wp_list_categories to list parent categories from actual term
-
16-10-2019 - |
Question
I currently have a taxonomy called wpsc_product_category. Under that taxonomy I have several terms used as sub-categories, and finally each sub-category has a number of products. I'm trying to use wp_list_categories to show an ul starting from the parent category of the current product your are viewing. ¿Is this possible?
<?php
$taxonomy = 'wpsc_product_category';
$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 = '';
$child_of = $actualcategoryparentid
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title
'child_of' => $child_of
);
?>
<ul>
<?php wp_list_categories( $args ); ?>
</ul>
I thought of something like this, but I don't know how to retrieve $actualcategoryparentid.
Any ideas?
Thanks!
Solution
this will work:
$terms = wp_get_object_terms( $post->ID, 'wpsc_product_category' );
foreach($terms as $term){
if($term->parent != 0){
// this category has a parent and its id is $term->parent
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange