-
-
Save tommcfarlin/6e5445d9d3d45da14fcf to your computer and use it in GitHub Desktop.
[WordPress] Determine if a given taxonomy term has children or does not have children.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 usecount()
.