0
votes

I have a piece of code that shows child terms of a custom taxonomy of the current parent term in the taxonomy-term.php template file. I want to display html only when there are child terms present. Now the heading 2 is displayed even when there are no child terms. Here is the code:

<?php
$term_children = get_terms('onderwerp', array( 'parent' => get_queried_object_id(),));
if ( ! is_wp_error( $terms ) || ($term_children->count != 0)  ) { ?>
    <section>
    <h2>Ik heb een vraag over:</h2>
    <div class="onderwerp-links">
    <?php
    foreach ( $term_children as $child ) { ?>
        <?php $img = get_term_meta( $term->term_id, 'ndftm_img', true ); ?>
            <a href="<?php echo esc_url( get_term_link( $child ) ) ?>" style="background-image: url(<?php echo $img; ?>)">
            <span><?php echo $child->name; ?></span>
            </a><?php
        }
        ?>
    </div>
    </section>
<?php
} 
?>
1

1 Answers

0
votes

I fixed it by adding

'hide_empty' => true

in the get terms array

and making the if statement like this:

if ( ! empty($term_children) )

Here's the code:

<?php
$term_children = get_terms('onderwerp', array( 'parent' => get_queried_object_id(), 'hide_empty' => true ));
if ( ! empty($term_children) ) { ?>
    <section>
    <h2>Ik heb een vraag over:</h2>
    <div class="onderwerp-links">
    <?php
    foreach ( $term_children as $child ) { ?>
        <?php $img = get_term_meta( $term->term_id, 'ndftm_img', true ); ?>
            <a href="<?php echo esc_url( get_term_link( $child ) ) ?>" style="background-image: url(<?php echo $img; ?>)">
            <span><?php echo $child->name; ?></span>
            </a><?php
        }
        ?>
    </div>
    </section>
<?php
} 
?>