-
-
Save tommcfarlin/475f3d28451809ffd43e5ffcb686e05d to your computer and use it in GitHub Desktop.
[WordPress] Get Taxonomy By Term ID
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 | |
/** | |
* Retrieves the taxonomy name associated on the specified $term_id. | |
* | |
* @access private | |
* @param int $term_id The term ID from which to retrieve the taxonomy name. | |
* @return string $taxonomy The name of the taxaonomy associated with the term ID. | |
*/ | |
private function get_taxonomy_by_term_id( $term_id ) { | |
// We can't get a term if we don't have a term ID. | |
if ( 0 === $term_id || null === $term_id ) { | |
return; | |
} | |
// Grab the term using the ID then read the name from the associated taxonomy. | |
$taxonomy = ''; | |
$term = get_term( $term_id ); | |
if ( false !== $term ) { | |
$taxonomy = $term->taxonomy; | |
} | |
return trim( $taxonomy ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment