Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Wordpress subcategories use Category Template

I've got a category template: category-projects.php

This category has subcategories, but they're refering to the template category.php for instructions instead of the parent category. How do I make subcategories refer to parent category templates in the cascading order of template references?

*Note, I'm talking about category level urls, not posts.

like image 479
Matrym Avatar asked Jun 25 '10 01:06

Matrym


People also ask

How do I show subcategories on a category page in WordPress?

Activate the plugin through the 'Plugins' screen in WordPress. On the 'Widgets' sub-menu of 'Appearance' you will find a new widget type called 'Sub Category'. Add one or more of these to your themes widget display areas. For each widget you add, decide what and how you'd like it to display.

How do I display all subcategories from a specific category in WooCommerce?

Click on Appearance > Customize. Then go to WooCommerce > Product Catalog. Select “show subcategories” from Category Display. Click on Save Changes.


1 Answers

Richard's answer does work but it will heavily interfere with other plugins.

I found a better alternative using add_filter & template_include as the example below

add_filter( 'template_include', 'my_callback' );

function my_callback( $original_template ) {
    if ( some_condition() ) {
        return SOME_PATH . '/some-custom-file.php';
    } else {
        return $original_template;
    }
}

Credit to https://markjaquith.wordpress.com/2014/02/19/template_redirect-is-not-for-loading-templates/

like image 144
Alvin Nguyen Avatar answered Oct 21 '22 08:10

Alvin Nguyen