At the top of a product archive page I am trying to display a list of product categories. The rule should be that the list always shows the direct children of the top level.
There are several answers on WPA which I have been trying to implement (with my minimal skills) but I am missing something.
Here’s the query:
<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$order = 'DESC';
$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;
// So now I need to get the top parent id in a variable. I tried these 4 options
/*
// option 1
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
// option 2
$thisCat = get_category( get_query_var( 'cat' ) );
while ( intval($thisCat->parent) > 0 ) {
$thisCat = get_category ( $thisCat->parent );
}
$term_id = $thisCat->term_id
//option 3
$parent = get_term_by( 'id', $term_id, $taxonomy);
while ($parent->parent != 0){
$parent = get_term_by( 'id', $parent->parent, $taxonomy);
}
//option 4
while ($catid) {
$cat = get_category($catid); // get the object for the catid
$catid = $cat->category_parent; // assign parent ID (if exists) to $catid
// the while loop will continue whilst there is a $catid
// when there is no longer a parent $catid will be NULL so we can assign our $catParent
$catParent = $cat->cat_ID;
}
*/
// Testing if I have the correct value
echo 'The returned variable is:'; print_r($term_id);
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => $term_id,
'orderby' => $orderby,
'order' => $order,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
$args = array(
//'type' => 'post',
'orderby' => 'term_group',
'hide_empty' => 0,
'hierarchical' => 0,
'parent' => 0,
'taxonomy' => 'product_cat'
);
$test = get_categories( $args );
//print_r($test);
$cat_id = get_query_var('cat');
echo $cat_id;
if (!empty($sub_cats)) {
echo '<ul class="categories">';
foreach($sub_cats as $sub_category) {
//echo 'de variabele sub_category'; print_r($sub_category);
if ($sub_cats->$sub_category == 0) {
$thumbnail_id = get_woocommerce_term_meta( $sub_category->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
$name = $sub_category->name;
$slug = $sub_category->slug;
echo '<li class="cat-item '.$slug.'"><a href="'. get_term_link($sub_category->slug, 'product_cat') .'"><img src="'.$image.'" " /><span class="txt">'.$name.'</span></a></li>';
}
}
echo '<li class="cat-item search">';
get_product_search_form();
echo '</li>';
echo '</ul>';
} //endif !empty ?>
Thanks!
Juri
In general to get the posts of your custom post type you need to query post_type, if you want only the categories you could just pull the categories instead of the entire post type.
Or you could use get_terms()?
Quick example:
Are you listing anything else on that archive page? If you are for example listing posts (or other information) from the same post type you could combine it.
EDIT:
I apologize I read over the comments in the script and thought you solely needed help with the rest. To just get the
term id
you could useget_terms()
(as referred above). And then take a look at the following parameters:Not including any other parameters it would look something like this:
You could also return all values and then just pull
term_id
.