• Resolved mike62

    (@mike62)


    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.

    Visual of 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!

    • This topic was modified 7 years, 7 months ago by mike62. Reason: clarity
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    I believe the problem is this part:

    $subcategories = get_categories( array(
        orderby' => 'name',
        'parent'  => $category

    $category here is the term object. Only the ID is expected for ‘parent’ Try setting ‘parent’ to $category->term_id

    Thread Starter mike62

    (@mike62)

    Yes! Thank you!!

    I had to do it here, to get the correct image, as well:

    
    $metaSubValue = wp_get_terms_meta($subcategory->term_id, 'category_image' ,true); 

    It’s working perfectly now. Thanks so much. ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Trying to loop through subcategories on a category.php template’ is closed to new replies.