首页 > 解决方案 > 如何在 index.php 中使用 is_archive() 来检索 WordPress 中基于存档的特定模板?

问题描述

我正在开发一个 WordPress 博客网站。

我正在尝试根据 WordPress 条件标签获取特定模板,如下所示:

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;

并且,它适用于前两个条件。但是,当我试图根据 is_archive() 条件标签获取位于 '/frontend/template-parts/archives/' 目录中的 category.php 模板时,这不是工作。

如何根据放置在 index.php 中的 is_archive() 条件标签动态设置位于“/frontend/template-parts/archives/”目录中的 category.php、tag.php、archive.php、author.php 等模板上文提到的。

谢谢你。

标签: phpwordpress

解决方案


试试下面的代码。

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;

推荐阅读