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
- Activate Gutenberg in category-descriptions
- How to show subcategories in categories else show posts
- How would I increase the Author Role to be able to add a new category?
- How to remove the parentheses from the category widget
- How to: 301 Redirect /category/ to /customname/
- Display one latest post from multiple categories
- Archive pagination causing 404 with permalinks structure
- Get a list of Terms for a specific category
- Remove Archive Headline and Archive Intro Text fields on category and tag archive pages in WordPress Admin with Genesis framework
- How Do I Merge Categories With phpMyAdmin
- Edit the markup of categories list
- Show category children, one level
- a-z list, categories and sub categories in loop
- Category slug $_SERVER[‘REQUEST_URI’];
- wp_dropdown_categories and custom taxonomy + custom post type
- Random taxonomy category list
- Show all posts for a specific category/author
- Resort get_categories
- How to call custom category field data in theme?
- One Specific Category To Permalink
- WordPress category widget only show categories with children
- Woocommerce custom category page with pagination [closed]
- Custom post and category template pagination problem
- How to list all categories in three divs dynamically based on the quatity of categories?
- Grouping categories by genre
- how to “not” display new posts with a specific category on the main page?
- Limit categories and it’s subcategories for specific group of users
- show all WordPress categories in a page
- getting post data in functions.php
- Category specific order
- Remove the number of posts displayed in wp_list_categories()
- Problems with my conditionals in single.php by category
- WP_safe_redirect not working
- Do not show sub categories in the loop on archive-product.php
- Having a template per category? Bad for server overhead?
- “Virtual category page” based on a custom field filter
- Anchor tag generates blank href attribute using echo esc_url( )
- Get categories related to particular category
- handling the featured category
- Exclude the category of the current post
- Output Title of Post’s Current Category
- Excluding current category from sidebar’s post widget, elementor, query-id
- OK to redirect the root WordPress category slug?
- Return all Tags and Categories in Separate List
- Featured Category or Special Category in WordPress
- WooCommerce REST API not returning the default products category
- Using the slug of a custom post category as prefix for the underlying custom post slugs
- Change background image page header overlay for each category
- List non-empty categories from a custom post type
- Creating a linkable category list with post count
- Limit Category/Tag Dropdown To blog_id in Multisite Install With Global Categories/Tags
- Custom built theme won’t filter categories
- WP_query exclude a category unless it has more than one catagory
- Narrow Down a Shop Page Results Based on a Product Tag in WooCommerce
- How to control order when displaying child categories
- Limit authors on specific catogories