php - 如何在 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 等模板上文提到的。
谢谢你。
解决方案
试试下面的代码。
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;
推荐阅读
- node.js - 上传文件时无法读取属性“路径”
- javascript - Angular 5:使用 forkJoin 一次多个请求
- android - onPause() 被意外调用
- php - MySQL 高 CPU 使用率会降低服务器速度,如何优化我的查询?
- azure - 无法将活动日志流式传输到事件中心
- spring-boot - 存储令牌以访问 Spring Boot 上的其他应用程序
- ng-show - 将 ID 发布到 JSON
- php - 为什么我会收到警告:“expects parameter 1 to be mysqli_result, string given”?
- c# - 如何在拉伸容器时保持画布固定大小
- vuejs2 - GeoJSON - onEachFeature 中的方法