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
- How can I hide a category from Contributors in the edit/add new post screen?
- Category page doesn’t use category.php, instead it redirects to homepage
- Multiple post categories – single permalink
- Delete a specific category when deleting a user
- 404 error on subcategory pages
- Add line breaks in Category description via enter
- Display only the latest post from multiple categories
- Mass/Bulk assign categories to posts
- how do I filter single_cat_title to remove all instances of a particlular word
- WordPress order categories by ID
- ACF: Display category name using taxonomy field
- How to show single category archive
- Shortcodes not working in category or tag description
- Post in multiple categories
- wp_dropdown_categories() scripts working erratically on sidebar
- Access current post parent and sub category
- How to exclude the last post from a category(featured for example) from the main loop?
- How to rewrite the post URL according to the category?
- Sticky post for each category on category pages [duplicate]
- Will renaming a `content.php` file cause any problems?
- Question on using custom structures for categories
- How to add orderby parameter for sorting on category.php
- delete category name in the pages
- How to show only the name of one category above each posts in frontpage?
- Tags Instead of Category
- Remove category link
- javascripts files not linking to my category slugs?
- Pretty URLs with Categories and Tags
- Select posts that match multiple category names, must match all categories listed
- How can i display category title over thumbnail?
- Need help with major category and permalink re-organization
- Include home page template in specific category template
- How to use category slug with a regular page
- Display all categories but only if they have posts in them that have a specific tag assigned
- Show audio player only in specific post type category
- display current portfolio categories from a specific parent
- selecting categories that have products with tag/category
- how to get only grandchild category from a child category
- How to hook into creating a category?
- Media Library Category Exclude Tree
- show posts in archive page based on selected catagory
- How to display child-category PAGES on a category page
- How get all media from a posts category by db?
- Get the categorie name of Event Organiser plugin [closed]
- get_categories returns top-level categories instead of child categories
- Show posts from all categories
- Display the category name of a post that is in two different categories
- how to Specifies an author / editor to edit one category only?
- (WordPress) How to get custom taxonomy parent name?
- Show one same template for two category
- My Query is getting the wrong data
- Make a menu visible for a single category and it’s content
- WordPress Loop Prob
- Display post content from category name using ajax
- WooCommerce category display in custom page
- How to display the category featured images [closed]