Wordpress custom taxonomy subcategory template

Viewed 59

I'm trying to create specific templates based on custom taxonomy subcategory slugs. Custom taxonomy is registered with CPT UI plugin.

An example explaining the problem:

Registered custom taxonomy: Fruits

Fruits has a category: Apples

Apples has a subcategories with slugs: red-apple, green-apple, yellow-apple

My goal is to use specific template for each subcategory: red-apple, green-apple and yellow-apple

What I have tried so far:

  1. I created a files such as taxonomy-fruits-red-apple.php, taxonomy-apples-red-apple.php etc - none worked.

  2. Used the following code:

    function wpd_subcategory_template( $template ) {
    $cat = get_queried_object();
    if ( isset( $cat ) && $cat->category_parent ) {
        $template = locate_template( 'my-template.php' );
    }
    
return $template;
}
add_filter( 'category_template', 'wpd_subcategory_template' );

None of these methods worked.

Any ideas?

Thx in advance!

0 Answers
Related