Last active
December 1, 2020 13:51
-
-
Save morgyface/aa0d54cee9c6f227c0424c5ef43a5f40 to your computer and use it in GitHub Desktop.
WordPress | Function | Get the post type the Taxonomy Term is related to
This file contains hidden or 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 | |
| function get_post_type_name( $term_id ) { | |
| // Here we use the term id to identify the name of the post type the related taxonomy is registered to | |
| $term = get_term( $term_id ); | |
| $taxonomy = $term->taxonomy; | |
| $tax_object = get_taxonomy( $taxonomy ); // Get the taxonomy object | |
| $post_types = $tax_object->object_type; // Get the object type (as an array) the taxonomy is registered to | |
| $post_type_object = get_post_type_object( $post_types[0] ); // Use the object type to get details on the object type | |
| $post_type_name = $post_type_object->labels->name; // Get the name label of the the object | |
| return $post_type_name; | |
| } | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To get the Term ID whilst on a category archive page use this: