Use get_ancestors()
and count the returned array: that’s the number of ancestors a term has.
/**
* Count a term’s ancestors.
*
* @param int $term_id
* @param string $taxonomy
* @return int
*/
function wpse_57512_count_ancestors( $term_id = FALSE, $taxonomy = FALSE )
{
if ( FALSE === $term_id and ! empty ( get_queried_object()->taxonomy ) )
{
$term_id = get_queried_object_id();
$taxonomy = get_queried_object()->taxonomy;
}
$ancestors = get_ancestors( $term_id, $taxonomy );
return $ancestors ? count( $ancestors ) : 0;
}
For a practical use case you may take a look at my plugin T5 Parent Terms in body_class. Its name should tell you what it does. 🙂
Related Posts:
- get_posts assigned to a specific custom taxonomy term, and not the term’s children
- Get the the top-level parent of a custom taxonomy term
- Determine Term depth
- Get taxonomy name of current post
- Determine if Term has Grandparent/Great-Grandparent
- Custom Taxonomy Only Showing Top Level Terms in Admin?
- How to set hierarchical terms to a post using wp_set_object_terms
- Hierarchical display of custom taxonomy
- Display hierarchical subterms of custom taxonomy based on depth
- Hierarchical Taxonomy Terms Select Menu Output with selected=”selected” Set
- Rewrite rules for varying nested terms of custom taxonomy
- List Posts For Terms Of A Custom Taxonomy For Any Post Type
- Taxonomy term breadcrumb; how?
- How can I get only parent terms?
- Retrieve posts by term id custom query
- How to prevent new terms being added to a custom taxonomy?
- Order terms by term_order
- Get value in custom field with taxonomy [closed]
- Get second level terms of custom taxonomy
- Large taxonomy (2000+ terms), causing WordPress to hang
- Is there a way to disable a term rather than deleting it?
- Modify Term Update Redirection
- Check if current term is a child of another term
- Is there a way to import terms into WordPress?
- get_terms() doesn’t return empty terms even though hide_empty is false
- wp_insert_term auto unique name
- Non-hierarchical custom taxonomy using checkboxes on edit-screen -> saving issue
- show term description instead of list terms of custom taxonomy
- Looping Through Custom Tax Terms and Displaying All Posts For Each
- How to get the top most term (top ancestor) of a custom taxonomy child term?
- Display Custom Taxonomy Terns ordered by meta_value
- get_the_term_list without specific category
- Listing child terms of parent term
- Display the taxonomy name of the current term
- Automatically assign taxonomy term if custom meta value exists
- Display the Terms from a Custom Taxonomy Assigned to a Post(inside the loop) in Hierarchial Order
- Edit post meta with checkboxes on front end
- Dynamically tax_query terms
- Populating dropdown menu with hierarchical taxonomies
- get_the_terms() to show all custom taxonomies
- How to order a list of taxonomy terms alphabetically?
- Custom Taxonomy Archive BUG
- Can I limit term selection to one plus parent?
- Insert form checkbox at bottom of taxonomy edit term page
- Get Current Custom Taxonomy ID by Post ID
- Changing stylesheet depending on custom taxonomy terms
- ‘wp’ action hook not firing in admin and login?
- Taxonomy archive showing no results
- How Can I Change a Taxonomy URL Based On The Originating URL?
- Get_terms stopped working after WP 4.2
- Changing taxonomy term by slug (wp_update_term)
- Getting Term ID from Term Name for WordPress Query
- Show only the grandchildren (using get_terms)
- Custom Taxonomy Term Caching?
- wp_insert_term created and set for first once only
- How to make custom post taxonomy looks like regular categories?
- List related terms + taxonomies
- Display all posts that use a custom taxonomy
- how to store values in database at hierarchical view
- Weekdays as terms – How to order taxonomy terms by ID in admin panel?
- Are term IDs unique even between multiple custom taxonomies
- What is the action hook that deletes a taxonomy term from the backend? And how to retireve the term id before deleting it?
- How to find taxonomy parent id from child taxonomy page?
- Why does wp_remove_object_terms not work?
- Display children category images
- Create alphabetical Pagination for custom taxonomy?
- save custom term – hook create_term doesn’t work
- URL rewriting taxonomy term
- How to show post for a particular term of custom taxonomy?
- Hide echo of no categories from get_object_taxonomies
- Show all terms in a custom taxonomy with all child terms wrapped in a ul
- Order taxonomy terms wordpress
- Unable to get the Parent Custom Taxonomy Terms
- How to get the term description in a taxonomy term archive query?
- Unsetting post_tag taxonomy breaks term description for other taxonomies
- How to list terms by first letter, as in A’s then B’s etc
- Pull data from MySQL and add it to terms
- Get custom taxonomy terms that contain posts that have another taxonomy term
- Cloning product not copying custom taxonomies
- Create new Taxonomy, add extra fields, register terms AND extra fields values?
- How to change custom categories term links?
- How to get a terms and posts associated with another term?
- Displaying subcategories and then posts in taxonomy template
- How to filter a taxonomy meta field to the ‘single_term_title’ filter hook
- Custom order of taxonomy using wp_get_object_terms and woocommerce_term_meta
- List terms and order by second word
- Query pages by child term
- Display taxonomy term slugs
- Tax query array terms display out of order
- Adding a query var to taxonomy term archive – gets redirected to the other taxonomy archive page
- displaying links if term is used
- How to include child terms within parent?
- Display term description on hover using get_the_term_list
- stdClass::$labels /wp-includes/general-template.php undefined
- List child terms and posts in the current term
- $term->taxonomy stripping out underscores
- Display woocommerce product_cat child terms with thumbnails
- List posts grouped by children of a custom taxonomy
- How to delete unused terms?
- Foreach for get_the_terms for hierarchical taxonomy don’t repeat Top Level Terms if contains multiple Second Level Terms