I’m using the taxonomy field to display categories from WooCommerce. I’d like to show the sub categories of the displayed category but unable to get these displaying automatically in a list below.
<?php
global $post;
$terms = get_field('product_range_categories');
if( $terms ): ?>
<ul>
<?php foreach( $terms as $term ): ?>
<li>
<div>
<a href="<?php echo get_term_link( $term ); ?>">
<h2>
<?php echo $term->name; ?>
</h2>
<?php
$thumbnail = get_field('category_thumbnail', $term );
if( !empty($thumbnail) ): ?>
<img src="<?php echo $thumbnail['sizes']['featured']; ?>" alt="<?php echo $thumbnail['alt']; ?>" />
<?php ?>
<?php endif; ?>
<?php if( $term ) {
foreach($term as $t) {
$t = get_category($t);
echo $t->name;
}
} ?>
</a>
<a href="<?php echo get_term_link( $term ); ?>">View the full range</a>
</div>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
The smaller loop inside sort of pulls a category from the parent that has sub categories, but it’s the post categories and not for WooCommerce
<?php if( $term ) {
foreach($term as $t) {
$t = get_category($t);
echo $t->name;
}
} ?>
Any help would be awesome. Thanks.
Hi @jordantrainor
The inner loop you have highlighted won’t work as expected.
The variable $term
is an object, not an array of sub terms.
To get the children of a term, you need to use some code like so:
http://codex.wordpress.org/Function_Reference/get_term_children
That function will allow you to load the child terms and then you can loop over them.