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
- 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?
- Determine if Term has Grandparent/Great-Grandparent
- How can I get only parent terms?
- Retrieve posts by term id custom query
- Change order of Custom Taxonomy List
- get_the_term_list without links in 3.1
- Inserting terms in an Hierarchical Taxonomy
- get_terms – only top level
- How to only list the child terms of a taxonomy and not their parents?
- Hierarchical taxonomy UI
- How to prevent new terms being added to a custom taxonomy?
- Custom taxonomy, get_the_terms, listing in order of parent > child
- Order get_terms using a Custom Field
- get_term_children for immediate children only (not grandchildren)
- Query users by custom taxonomy and user role
- How to get current term in my custom taxonomy in WordPress?
- Is there a way to ‘Lock’ a Taxonomy?
- Hide the term description on the term edit page, for a given taxonomy
- Order terms by term_order
- get_term_by not working when in functions.php
- Creating a non-removable taxonomy term
- Get value in custom field with taxonomy [closed]
- Get second level terms of custom taxonomy
- Can I lock down custom taxonomies on a parent term level, but not a child term?
- Get only the first term by taxonomy
- Exclude specific slug in ‘get_terms’
- Large taxonomy (2000+ terms), causing WordPress to hang
- Custom order of terms for custom taxonomy in admin and website
- Is there a way to disable a term rather than deleting it?
- Modify Term Update Redirection
- Best pratice to make taxonomy terms translatable without changing slugs?
- Front End Post Save Child terms
- Return only the custom sub-term for custom post type, do not echo term-parent
- wp_insert_term is adding a term that has no name
- Taxonomy slug by term ID
- How to enable hierarchical permalinks for hierarchical taxonomies
- 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
- Get terms from multiple taxonomies
- Formating the_terms() function output
- get_terms() returns an empty array
- wp_insert_term auto unique name
- get_terms showing link to category even if all posts are drafts
- Custom Taxonomy Relationship (ex: plant classification)
- Dropdown: Display terms from B only if has relationship with a term A selected
- Non-hierarchical custom taxonomy using checkboxes on edit-screen -> saving issue
- Retrieving custom taxonomy in order, but excluding specific tax IDs
- wp_get_object_terms – How can I order the resulting array by hierarchy?
- Get main parent categories for a product
- Get current term’s ID
- Is it possible to get a Previous/Next Taxonomy term archive URL?
- Showing posts by collection of specific terms and texonomy
- How to fix a double slash in custom permalinks with hierarchical taxonomy’s?
- get taxonomy terms for parent and child
- Taxonomy query for children of parents
- show term description instead of list terms of custom taxonomy
- WP_insert_term how to insert multiple values as taxonomny term?
- Looping Through Custom Tax Terms and Displaying All Posts For Each
- How to include parent terms in hierarchical taxonomy URLs?
- Group posts that matches a term in a loop
- term_link() filter or rewrite and howto?
- How to get the top most term (top ancestor) of a custom taxonomy child term?
- Display Custom Taxonomy Terns ordered by meta_value
- Separator for multiple terms
- Contact Form 7 – Populating dropdown list with terms relative to the post
- Custom Taxonomy terms with latest post ordered by date pagination issue
- Help building array hierarchically with taxonomies (to get posts)
- Echo taxonomy term meta on author.php
- get_the_term_list without specific category
- Listing child terms of parent term
- Display the taxonomy name of the current term
- Custom taxonomy term as class?
- How to edit/remove a term that’s in multiple taxonomies?
- Ordering Posts with Custom Taxonomy Terms Array
- How to create a theme template for the taxonomy “root”
- How to display term description in empty terms archive?
- Advanced custom fields – taxonomy terms images [closed]
- Checking if a Page has an Associated Term?
- Custom taxonomy term in WooCommerce product permalink
- What are terms and taxonomy, how they related to post and how these three are stored in database?
- Get multiple term objects by ids
- Automatically assign taxonomy term if custom meta value exists
- List only first-level children of specific custom taxonomy term
- Echo custom taxonomy term name
- get_terms of post->id
- How to handle “the_terms” inside loop
- get_terms custom order
- Display the Terms from a Custom Taxonomy Assigned to a Post(inside the loop) in Hierarchial Order