php - 将我的帖子从 single.php 导航到自定义模板
问题描述
我的网站上有一些帖子是通过 single.php 发布的,现在我有一个新页面。其中有一些我想使用自定义模板的帖子,但我的帖子是 single.php 模板。谁能帮我写一些我可以在 single.php 中编写的代码,以便我可以使用帖子 ID 进行导航。?在我的自定义模板中,我应该使用什么代码来构建它..?
解决方案
单.php
<?php get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
if (have_posts()) : while (have_posts()) : the_post();
$custom_arr = array(1,2,3,4);
if (in_array(get_the_ID(), $custom_arr)) {
get_template_part( 'parts/loop', 'single-custom' );
}
else {
get_template_part( 'parts/loop', 'single' );
}
endwhile; else :
get_template_part( 'parts/content', 'missing' );
endif;
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_footer(); ?>
然后创建两个单独的文件/parts/loop-single.php
和/parts/loop-single-custom.php
而不是通过 id 映射,您可以通过自定义类别或帖子上的自定义字段进行检查,硬编码 ID 的数组只是不好的做法
推荐阅读
- sparql - 使用 SPARQL 查询从 Neptune DB 检索数据
- r - 无法在 shinyapp.io 上创建新目录
- javascript - 无法使用 React Redux-Saga 从 firebase child 获取数据?
- c# - 平展的 PDF 表单字段在保存时更改字体
- php - 如何检查异常
- c# - .net 5 未播种数据
- vue.js - 带有 Mux 的 Video.js 不播放视频
- java - 如何使用可变参数重构代码 - 放到地图上
- c# - 通过 Azure(Microsoft Bot Framework)发布时数据文件不存在
- aws-api-gateway - Lambda Authorizer Policy 不限制对 Api Gateway 代理资源的访问