最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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
}

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
}
Share Improve this question asked Oct 23, 2020 at 13:16 Vincenzo PiromalliVincenzo Piromalli 1989 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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;
}
} else { 

}
发布评论

评论列表(0)

  1. 暂无评论