0

I have a custom taxonomy with two levels of terms.

  • Parent term
    • Child term
    • Child term
  • Parent term
    • Child term
    • Child term
  • Parent term (no children)

I'm using a custom archive.php template with some HTML that I only want to display on terms without child terms.

Here's what I've tried...

$taxonomy = 'custom_tax';
$term = get_queried_object();

$children = get_terms( $term->taxonomy, array( 'parent' => $term->term_id ) );

if(!$children) {
    echo '<p>HTML only terms without child terms</p>';
}

This works if it's a Child term but not on a Parent term with no children!

Any help please?

0

You can use get_term_children for checking there is have any child term or not.

$taxonomy = 'custom_tax';
$term = get_queried_object();
if ( count( get_term_children( $term->term_id, $taxonomy ) ) === 0 ) {
     echo '<p>HTML only terms without child terms</p>';
}
else
{
    echo '<p>HTML only terms with child terms</p>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.