DEV Community

Vi 🐥
Vi 🐥

Posted on

How can i conditionally load taxonomy-{taxonomy}.php template files through functions.php

I have a movie review site, i would like to conditionally load the taxonomies of actors and directors only if a option of my theme is active. At the moment in the root theme folder I have these 2 files

taxonomy-actors.php
taxonomy-directors-php
I would like them to be used only if the function is active for example

if ($theme_comments == 1)
{
//load taxonomy-actors.php
} else {
//don't load taxonomy-actors.php
}

Top comments (1)

Collapse
 
vipulcino profile image
Vi 🐥

Find the solution :)

Code:
if ($moviewp_comments == 1) {

add_filter( 'template_include', 'wpse_template_include' );
function wpse_template_include( $template ) {
// Handle taxonomy templates.
$taxonomy = get_query_var( 'taxonomy' );
if ( is_tax() && $taxonomy ) {
$file = get_theme_file_path() . '/templates/taxonomy-' . $taxonomy . '.php';
if ( file_exists( $file ) ) {
$template = $file;
}

}

return $template;
Enter fullscreen mode Exit fullscreen mode

}
} else {

}

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

AWS GenAI Live!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️