简体   繁体   中英

How can I use is_archive() in index.php to retrieve specific archive based template in WordPress?

I'm developing a WordPress blog site.

I'm trying to get a specific template according to WordPress conditional tags as written bellow:

switch(1):
    case(!is_single() && get_post_type() == 'post'):
        get_template_part('/frontend/template-parts/general');
        break;
    case(is_single() && get_post_type() == 'post'):
        get_template_part('/frontend/template-parts/single/single', get_post_format());
        break;
    case(is_archive()):
        get_template_part('/frontend/template-parts/archives/category');
        break;
endswitch;

and, it works fine for first two conditons. But, It's not workig, when I'm trying to get the category.php template located in '/frontend/template-parts/archives/' directory based on is_archive() conditional tag.

How can I set templates like category.php, tag.php, archive.php, author.php dynamically, located on '/frontend/template-parts/archives/' directory based on is_archive() conditional tag placed in index.php as mentioned above.

Thank you.

Try Below code.

switch(1):
    case(!is_single() && get_post_type() == 'post'):
        get_template_part('/frontend/template-parts/general');
        break;
    case(is_single() && get_post_type() == 'post'):
        get_template_part('/frontend/template-parts/single/single', get_post_format());
        break;
    case(is_archive()):
        get_template_part('/frontend/template-parts/archives/archive');
        break;
    case(is_category()):
        get_template_part('/frontend/template-parts/archives/category');
        break;
    case(is_tag()):
        get_template_part('/frontend/template-parts/archives/tag');
        break;
    case(is_author()):
        get_template_part('/frontend/template-parts/archives/author');
        break;
endswitch;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM