Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] Determine if a given taxonomy term has children or does not have children.
<?php
// First, read the reads
$materials = wp_get_post_terms( get_the_ID(), 'acme_materials' );
foreach ( $materials as $material ) {
if ( count( get_term_children( $material->term_id, 'acme_materials' ) ) > 0 ) {
// The term has children
}
}
<?php
// First, read the reads
$materials = wp_get_post_terms( get_the_ID(), 'acme_materials' );
foreach ( $materials as $material ) {
if ( count( get_term_children( $material->term_id, 'acme_materials' ) ) === 0 ) {
// The term has no children
}
}
@swissspidy

This comment has been minimized.

swissspidy commented Jan 6, 2015

Small typo: In the first example, // The term has no children should be // The term has children :)

Besides that, I'd probably just use if ( get_term_children( ... ) ) since this boolean check equals false for an empty array. No need to use count().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment