Try get_ancestors()
:
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$ancestors = get_ancestors( $term->term_id, 'product_range', 'taxonomy' );
$hierarchy_levels = count( $ancestors );
switch ( $hierarchy_levels ) {
case 0:
// THIS IS THE PARENT CATEGORY
// DO YOUR STUFF
break;
case 1:
// THIS IS THE PARENT CATEGORY
// DO YOUR STUFF
break;
case 2:
// THIS IS THE PARENT CATEGORY
// DO YOUR STUFF
break;
}
Another solution (but constraining) would be determining which hierarchy the user is currently on based on the url structure. The constraint would be that you have to keep a “simple” or “classical” hierarchy structure in your category URLs, as follow:
https://example.com/parent/child/grandchild/
https://example.com/parent/child/
https://example.com/parent/
Related Posts:
- WordPress Doesn’t Generate Taxonomy Archive
- Use the custom post type archive for taxonomies?
- Single taxonomy for different custom post types
- Show custom category archive as front page and remove taxonomy slug from urls
- Custom Taxonomy as checkbox or dropdown
- Taxonomy: Why ‘with_front’ => false DOES NOT WORK?
- Permalink Structure for Multiple Post Type Archives by Taxonomy
- Custom Taxonomy as Dropdown in admin
- Custom taxonomy not showing up when adding a new custom post type
- How to list all categories and tags in a page?
- Create permalink structure for one taxonomy archive per custom post type
- Retrieve single term slug
- Create an archive page for custom taxonomies
- Query multiple taxonomy in Custom Post Type
- Custom Post Type: How to display all of same taxonomy?
- Disable single pages and archives and keep preview
- Remove “-2” from a Toolset Types URL with the same post name
- Trying to manage templates on a blog with lots of custom taxonomies
- Associate Taxonomies Across Post Types
- Use the_taxonomies() to create a simple list
- Listing taxonomies from custom post type
- Custom Post Type and Taxonomy combination
- Order WordPress Custom Taxonomy Pages & Pagination Not Working
- Custom post type and taxonomy permalinks – Structure
- Relationship between two taxonomies and a custom post type [closed]
- How to check the terms in single custom post type template
- How can I move a custom taxonomy and its data from one post type to another?
- Hierarchical Custom Post Types – Show only parent on tax archive?
- Archive page for taxonomy terms
- Custom post type templating problem
- Custom Taxonomies not appearing in Admin
- How to Filter custom post type by taxonomy?
- Limit amount of posts made within a custom taxonomy
- Issue on Creating Custom Tax Term Dynamically From Another CPT Meta-box On Publish
- list taxonomies from a custom post type
- How do I display the grand child items of a taxonomy term?
- Taxonomies relations
- Is it better to use WordPress Custom Post Types or Taxonomies?
- Full Custom Post Type List Organised by two Taxonomies
- Adding custom taxonomy in same menu place with two custom post types
- Archive for a Taxonomy of a Custom Post type
- Custom Taxonomy Is Being Pulled into a Page, But It Doesn’t Have A Hyperlink
- Querying two taxonomies with tax_query not woking
- Custom Post type & Taxonomy 404
- Hide specific taxonomies from a taxonomy list using ‘get_object_taxonomies’
- Custom taxonomy (categories) on custom post type return no results
- get_category_link() for custom post type does not include custom slug rewrite?
- Targeting categories in custom fields
- Custom Post Types not queried in Custom Taxonomy archives or Native archives
- Rewrite custom post type with taxonomy
- My custom taxonomy is only displaying 1 of 3 terms
- Changing CPT slug and taxonomy already registered in parent theme
- Custom Taxonomy Terms in Menu lead to which page?
- Archive page for custom post type and custom taxonomy
- Using page title as a link to term archive
- How to display posttypes and taxonomy in standard posts, not in a separate label?
- Custom post types templates
- Advanced archive url structure (category, tag and date)
- “Menu order” being ignored when querying posts from a parent taxonomy with “include_children” set to true
- Get the taxonomy value in the post loop inside the archive page
- How to show related posts of category on post within custom posttype
- Custom Post Type Archive Page showing as 404
- Custom taxonomy returns 404 even with saving permalinks
- How to add CSS class field in Custom Taxonomy?
- WP_Query for custom taxonomies showing posts from non-specified terms?
- How to show custom post count in archive page
- Set a Default CPT taxonomy by taxonomy id
- Archive for custom taxonomy
- Listing all custom posts having a specific taxonomy whatever the terms
- How to Use A Custom Post Type as Taxonomy for Another Custom Post Type
- How to manage a dynamic multi-level page hierearchy system?
- Display custom tax in “while” loop
- Categories manage
- How to get post type archive category title
- taxonomy – templates are not loading
- Showing taxonomies with terms that are attached to custom post
- Templates for CPT not working
- Custom Taxonomy page redirecting to 404 page
- Changing an item in drop down after 5 latest posts per taxonomy
- Filter Term By Parent Term – Custom Post Type
- Displaying WordPress posts from post and custom post type in custom taxonomy
- Custom post type taxonomy template and URL confusion
- Custom Taxonomy is disabled on Edit page
- Override “Parent” input for custom taxonomy
- archive-{custom_post_type}.php not getting recognized wordpress
- Connecting multiple post types? Or linking “News” posts with “Game” posts how?
- How to display custom post types AND regular posts separately on a shared taxonomy archive?
- Sort by Custom Post Type (Multiple Loop)
- How to display elements of different post types?
- How to display only taxonomies of custom post type in a page?
- Check if taxonomy is attached to at least one post in a post-type
- Looping taxonomy in taxonomy?
- Custom posts and custom taxonomies for many products and categories?
- get_the_term_list() wanting to loop through the returned values
- How do i get the taxonomy term name on the CPT archive page?
- Get all active posts that are tied to a custom taxonomy for a custom post type
- How to add to taxonomies to a post type
- Filtering custom post types using category taxonomy
- Is it possible for post and custom post type to share a category base slug?
- Taxonomy Archive URL + Template