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
- WordPress Multisite – global categories
- Get posts from sites in Multisite?
- Can’t custom taxonomies have same terms (slugs) as categories?
- Using wp_category_checklist in a widget
- Filter front page posts by category
- Exclude one category from get_the_term_list
- WordPress remove separator from last item in wp_list_categories
- Need help adding custom field to category
- How to Include the Parent and Child Category in the Permalink if the Post is Added to Both
- Number of posts in page – set per category and exclude sidebar
- Create single.php for specific category by category id
- is_category in pre_get_posts results in php notices
- Use get_term_children to get the sub category of a parent category for the current post
- How to make all posts in a category “unsticky”?
- Get a variable from object in array [closed]
- How to stop wordpress from showing the selected category on top of others in the category selection?
- advanced custom field Show field in special categry page [closed]
- Exclude Child Categories Using wp_list_categories
- How do I get a list of all categories that a given user has written blog posts for?
- How to remove “Category : …”
- Add “page-a” slug to category link if user visit category link from “page-a”
- Query Posts Exclude Entire Category
- Hide woocommerce category name from specific pages
- select parent category first then child
- Displaying Child Categories in carousel
- Is there an easy way to get a list of Category IDs?
- How to customize my category pages
- Remove “Browsed by Category” from Category Page
- How to display a value inside a post with a specified category from a category custom field? [closed]
- Apply WordPress function on specific woocommerce product categories only
- display specific category on page
- Media (images, videos, etc.) not showing on Category page
- Problem with Custom Post Type Categories
- Display post thumbnail for specific category outside the loop
- PHP dynamical conditional post display
- wp_dropdown_categories initial value
- Displaying categories in different template problem
- Limit categories to hierarchal depth of 2
- Get_term_meta() does not work with pre_get_posts()
- How can I exclude a particular category from my WordPress Page 1 and Page 2?
- How can I add metaboxs to Admin Bar?
- How to add Pagination to foreach loop to page
- get_terms sort order with child categories of varying depth
- How to get posts from many categories using WP_Query
- Category slug in in loop always the same?
- Displaying posts by year
- How to get the post of category
- Logic for Nearby places with post and categories
- WordPress a template for subcategories fo a given category, but not for root category
- Image menu showing categories in page
- Is it possible to know which category page the post was linked from?
- Category Archives: Show posts categorized in parent category only
- Change Category Page Display
- Call Current Category
- Country Ways Content Show
- Add Category Descriptions to Montezuma Theme