Trying to loop through subcategories on a category.php template
-
I am trying to achieve the following on my category.php template:
– Check to see if there are subcategories
– For each subcategory for the current category, display:
— The heading of the subcategory
— All of the subcategories of THAT subcategory, as links.In the mockup below, the main category is the “Shop” category (shown at the top). Then, for each subcategory, there would be one row, like the “Our House” category, shown here. The “Our House” category has several subcategories.
I’ve got all the subcategories set up, and the category thumbnails are working too. But when I try to loop through these subcategories using the below code, I get the following error:
Warning: Illegal offset type in isset or empty in /path_to_root/wp-includes/class-wp-term-query.php on line 380
Here’s the relevant code that is causing the error. I’m hoping there’s a simple syntax error somewhere? I’m an intermediate developer, and looping through two levels of categories like this is new to me.
<?php // show a slider for each category, with clickable thumbanils for each subcategory $categories = get_categories( array( 'orderby' => 'name', 'parent' => $cat ) ); if ($categories) { foreach ( $categories as $category ) {?> <div class="container alternaterow blog-subcategory"> <div class="container-inner"> <?php echo '<h3><a href="'; echo $category->slug; echo '">'; echo $category->name; echo '</a></h3>'; echo '</div>'; ?> <div class="slick-slider"> <?php $subcategories = get_categories( array( 'orderby' => 'name', 'parent' => $category ) ); if ($subcategories) { foreach ( $subcategories as $subcategory ) { $metaSubValue = wp_get_terms_meta($subcategory, 'category_image' ,true); if ($metaSubValue) { echo 'div'; echo '<div class="featured-image"><img src="'; echo $metaSubValue; echo '" /></div><!-- .featured-image -->'; echo '<h3><a href="'; echo $subcategory->slug; echo '">'; echo $subcategory->name; echo '</a></h3>'; echo '</div>'; } } } ?> </div><!-- .slick-slider --> </div><!--.container-inner --> </div><!--.container --> <?php } } ?>
Any help would be appreciated, thanks!
- The topic ‘Trying to loop through subcategories on a category.php template’ is closed to new replies.