The template hierarchy has filters for all types of templates. Here we can use category_template, check if the current category has a parent, and load the subcategory.php file in that case:
function wpd_subcategory_template( $template ) {
$cat = get_queried_object();
if ( isset( $cat ) && $cat->category_parent ) {
$template = locate_template( 'subcategory.php' );
}
return $template;
}
add_filter( 'category_template', 'wpd_subcategory_template' );
Related Posts:
- Different template for subcategories
- How to display non-hierarchical taxonomy as terms with checkboxes?
- How to make child categories recognize parent’s template displays
- Different templates for parent and children categories/taxonomies
- Categories’ hierarchy in URL
- How to only show posts on last child category
- Categories lose hierarchy order once assigned to post
- is_archive() content being shown instead of is_category()
- Which php file lists all the post of a category
- Add Product categories to WordPress menu without losing hierarchy
- How can I get an tag to wrap each ancestor that gets outputted in this condition?
- how to get the categories for a single post in a hierarchical way
- Category tree is flattened inside admin upon saving
- Categories vs. Page Site Organization
- How to enable category hierarchy for links?
- Display Categories, Sub-categories, and Sub-sub-categories on separate pages
- Category-slug.php not being used
- Combining post categories and link categories?
- How to categorize posts under Category 1 and Category 2 which are separate lists of categories, and display them as separate lists in widgets
- Give a page priority over a category that has the same name?
- Categories overview that links to page displaying posts
- Add child category to parent on post creation and save
- How do I make the category sidebar widget (stock) display the total # of posts including those in subcategories?
- How to get Next/previous_post_link to go through articles in top parent category
- Hierarchical permalink structure for posts showing categories and sub-categories
- WordPress list categories dropdown with parent-child relation and child under it’s parent
- What Must to Display the “mysite.com/category” URL?
- Template hierarchy: how to let category.php conditionally-load archive.php?
- Display only subcategories of a category and not sub-subcategories
- How to load several hierarchical categories from functions.php
- Check is category parent with ids from the childs – get_term_children
- Posts not listing out sub category in hierarchy
- list all child categories that apply to current post even if those cats are empty?
- Show subChild categories
- Parent Category and Subcategory default text generated with category
- Custom Archive Template for Multiple Categories
- outputting taxonomy hierarchy
- Use different file includes for single.php
- Most efficient way to have 1 template for parent, 1 template for child categories?
- Children categories not shown in dashboard
- Most efficient use of custom taxonomies, categories, and pages
- Display categories and their IDs
- Template files not working for archives and categories
- List the category tree of all the product_cat categories
- Group WP_Query by category
- How to add the category ID to admin page
- Multiple Domain Names – One WP Install (non-Multisite) – Default Each Domain name to Category Archive
- Filter categories using tags
- Sticky posts on home page, search, tag and archives without plugin
- Show Post from parent category (custom taxonomy) ONLY!
- How do I get the total number of categories in a list of search results?
- Update wordpress post terms programatically
- Display products for a category, sorted by post_excerpt
- Creating archive pages for children categories
- “NOT ONLY IN” taxonomy query operator?
- How to remove the “+Add Category” button from the Category metabox?
- Display the featured image from the last post
- Arrange posts by date in front page
- “Categories” on my front page [closed]
- Warning: sizeof (): Parameter must be an array or an object that implements Countable, On products pages
- How to loop only categories without posts (+ show category featured image with acf) [closed]
- How can I create a Category Index page that lists all categories?
- Changing permalink from /postname/ to category/postname/
- Scheduling update post daily
- Retrieving category’s color with get_term_meta
- Assign category from custom field on post creation and update
- Sticky posts not working when a specific category is set
- Does “show_option_all” in wp_list_categories do anything?
- redirecting from a general category page to another specific one
- Categories are not listing their respective products
- how to nest specific category thumbnails inside different containers
- Is there any way to check which wordpress categories was created by which user?
- Adding a block with query in homepage
- multiple values in an array for category__and does not work with WP_Query
- Category view with contents of [gallery]s shown
- How can I include custom category and tag base in template files?
- query_post problem
- Add properties to term object
- Display all Categories
- custom wp_list_categories in single.php
- How to target all woocommerce categories that don’t have any subcategories in them?
- Is this process OK to replace URL structure?
- How can i echo next and previous custom category from a Taxonomy
- How to check a category checkbox using JavaScript
- Need help to structure our a complex hierarchy
- create post with main category and other categories from admin panel
- How Can Hide Define Category in Post Contents?
- How to auto update post title and slug with category name when post status is updated
- get_posts – require all categories
- 404 error when i try to search by category or by tag
- How do you add Author and date Published on Category Pages?
- hierarchical taxonomy in permalinks? using same slug
- WordPress Numeric Pagination with Query String [duplicate]
- display certain category on custom template page
- Get post by Category in custom template
- Ajax call activate after submit in edit-tags.php page
- Assign a category by user and customize the edit-tags.php?taxonomy=category page
- comments_number fails to print
- How to show child category name & post from it’s parent [closed]
- Open everything in new tab