Skip to content

Instantly share code, notes, and snippets.

@morgyface
Last active December 1, 2020 13:51
Show Gist options
  • Select an option

  • Save morgyface/aa0d54cee9c6f227c0424c5ef43a5f40 to your computer and use it in GitHub Desktop.

Select an option

Save morgyface/aa0d54cee9c6f227c0424c5ef43a5f40 to your computer and use it in GitHub Desktop.
WordPress | Function | Get the post type the Taxonomy Term is related to
<?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;
}
?>
@morgyface
Copy link
Copy Markdown
Author

To get the Term ID whilst on a category archive page use this:

if( is_category() ) {
    $title = single_cat_title( '', false );
    $term = get_term_by('name', $title, 'category');
    $subtitle = get_post_type_name($term->term_id);
}

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