【问题标题】:How can I use is_archive() in index.php to retrieve specific archive based template in WordPress?如何在 index.php 中使用 is_archive() 来检索 WordPress 中基于存档的特定模板?
【发布时间】:2021-04-07 18:37:33
【问题描述】:

我正在开发一个 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 模板时,这不是工作。

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

谢谢。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    试试下面的代码。

    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;
    

    【讨论】:

    • 谢谢,@Bhautik。我以前试过。但是,它不起作用。
    猜你喜欢
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 2016-09-08
    • 2022-08-21
    相关资源
    最近更新 更多