This query will help you
global $wpdb;
$cat_id = 65;
// this query will get parent relationships from term_taxonomy table and get category names from terms table
$category = $wpdb->get_row(
"SELECT t4.term_id as parent_id, t4.name as parent_name, t5.term_id as grandparent_id, t5.name as grandparent_name FROM `{$wpdb->prefix}term_taxonomy` t1
left join `{$wpdb->prefix}term_taxonomy` t2 on t2.term_id = t1.parent
left join `{$wpdb->prefix}term_taxonomy` t3 on t3.term_id = t2.parent
left join `{$wpdb->prefix}terms` t4 on t4.term_id = t2.term_id
left join `{$wpdb->prefix}terms` t5 on t5.term_id = t3.term_id
where t1.term_id={$cat_id}"
);
if ($category->grandparent_id) {
echo "blog > {$category->grandparent_name} > {$category->parent_name}";
} else if ($category->parent_id) {
echo "blog > {$category->parent_name}";
}
Related Posts:
- Control term order on a per-post basis
- Display all posts from all categories with pagination
- is_category() function
- Changing Top Level Items into Radio Buttons in the Categories Meta Box?
- Create terms when registering taxonomy?
- How to create a widgetized sidebar for every category dynamically?
- Display the deepest child category from Category X (in loop)
- Remove “Category Archives: title” at the top of a category page [duplicate]
- When using get_categories or similar, is it possible to filter results that contain certain Tags as well?
- Category Alphabet List Broken
- WooCommerce – Display product child categories from specific parent
- Select category in custom query
- Removed slug from CPT, now How/where do I hook the filter to the taxonomy term archive pages link?
- Display only one result from “get_the_category_list”
- Display empty taxonomy terms with get_terms()
- Show list of categories even if they have no posts
- Assign an icon to a category post
- How can I show the subcategories content on the parent category page?
- How to control template resolution if both Author and Category filter in place?
- Resolved – category-slug.php not working after wordpress migration
- How to retain the values from dropdown category lists after wrong form submission?
- Category archive page – loop through posts of certain tag (with pagination) – pre_get_posts
- Format Brand value in Category view [closed]
- Get multidimensional array that reflects category hierarchy
- Single_cat_title() print the title before text
- Add Post Screen Keep Category structure
- 3 Columns, 3 Categories, One Archive, and Pagination
- Possible to make a subdirectory that loops through a single category?
- editing fonts of category links from the_category() funtion
- Archives for each category on WP Multisite
- Update post title with category name after editing a category
- Automatically set default password to all posts in a specific category
- Style wp_list_categories
- Warn me about ‘uncategorized’ posts
- Categories showing page not found
- Why category.php throw 404 in wordpress while calling paginate_links()?
- How to get related tags based on a category?
- Show category to visitors in different name or different language without changing basic category
- Send to different single pages by category when multiple categories exist
- How to get latest posts from each category in regular order
- How can you change the permalink for pages?
- Pagination for ajax category filter
- Get posts from specific taxonomy term
- limit a list of tags to one category [closed]
- WordPress Drop down category as array for using in page builder
- Get sort order of product categories
- How to select category on if function
- how to wordpress When I click on the category link, subcategories appear
- woocommerce related products by specific categories [closed]
- Problem with different query loops (and “main loop”) on category template page!
- Show posts in “image” format when viewed in category page, but with comments in blog
- Change the name Category – Not change category name
- How to change the order of the subcategory and category in a post?
- Properly setting up a feed
- Display posts of certain categories to specific user roles
- Display Related Posts by “Category” “by Author”
- Change post title if post has specific category
- How to schedule categories?
- Template for product-category page [closed]
- How set all posts in wordpress in one category
- Copy post taxonomy?
- custom query for both category and pages using global $wpdb;
- url for posts for a date within a category
- Display category and associated post listings
- Autolinking keywords in text with tags
- How to get sub-categories to display same look as there parents
- Combining these queries? (Same taxonomy term + same category as post)
- get_object_term_cache duplicate terms after update to wordpress 6
- WordPress PageNavi Plugin not working in category.php
- Ignore the Ad on Specific Categories
- Different post slug based on archive
- How do I get conditional sidebars for specific category archives and posts
- Post does not show category wish just show latest post
- Sidebar by Category Conditional Statement not functioning
- Style post archive blog preview based on category
- Woocommerce Category Sort Dropdown
- Change the full slug for a specific category?
- Loop by category and meta_key
- Is there a way to ‘archive’ inactive categories?
- Hiding by default posts in given category except for some cases
- Making my category headers smaller
- How to display get_categories selected category?
- Hide category name using mod_rewrite
- Automatic Category selection
- the_category if/else statement
- DESC order not working when showing only child categories
- exclude parents from the_terms
- Search tag within category
- How to create category page
- Exclude Category From Home Page, Display Posts on It’s Own Page?
- Custom WordPress category page showing all posts rather than the specified category
- How to get child category list post in one template?
- i want to add pagination list of categories
- Woocommerce custom taxonomy order
- Update wordpress menu with woocommerce products
- How to show child category name & post from it’s parent [closed]
- Open everything in new tab
- hide_empty property not working when using get_terms
- Category Redirection Problem
- Hierarchical outline of posts