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?
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?
Share Improve this question asked Oct 23, 2019 at 6:42 ob80ob80 11 bronze badge1 Answer
Reset to default 0You 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>';
}