如何为自定义帖子类型归档正确使用 <header>/<footer>?
解读
在国内企业面试里,这道题表面问“怎么用 header/footer”,实则考察你对 模板层级、模板复用、CPT 注册参数、钩子优先级、主题兼容性与性能 的综合理解。面试官希望你证明:
- 不会让 CPT 归档走到 index.php 或 404;
- 不会把 header.php 直接复制一份造成维护灾难;
- 不会用 init 钩子做模板判断导致查询未就绪;
- 不会把 SEO 关键信息(canonical、og:title)漏掉;
- 不会把 5 MB 的 hero 图片硬塞给移动端。
一句话:让归档页正确复用主题的 header/footer,又能注入 CPT 专属逻辑,同时保证可升级、可缓存、可测试。
知识点
- 模板层级:archive-{post_type}.php → archive.php → index.php
- get_header( name ) 的 $name 参数机制
- CPT 注册:has_archive、rewrite、publicly_queryable、query_var
- pre_get_posts 与 is_post_type_archive() 的条件判断时机
- body_class 过滤器 追加自定义类,保障 CSS/JS 隔离
- do_action( 'get_header', name ) 差异
- 主题兼容:子主题、block-based theme(theme.json)对 header 的覆写规则
- 缓存与性能:Nginx fastcgi_cache / Redis 对 ESI 片段缓存 header 的注意事项
- 安全:header 中输出自定义查询变量时必须 esc_attr / esc_url
- 国际化:header 内嵌的 wp_title 过滤器已被废弃,应使用 document_title_parts
答案
- 注册 CPT 时显式开启归档
add_action( 'init', function () {
register_post_type( 'course', [
'public' => true,
'has_archive' => 'courses', // 固定链接 /courses/
'rewrite' => [ 'slug' => 'course', 'with_front' => false ],
'publicly_queryable'=> true,
'query_var' => true,
'supports' => [ 'title', 'editor', 'thumbnail' ],
] );
} );
- 创建归档模板 archive-course.php(与 CPT 同名自动识别),放在子主题或插件的 templates 目录,并通过 template_include 过滤器兜底映射,防止主题升级丢失。
// 插件内映射模板
add_filter( 'template_include', function ( $template ) {
if ( is_post_type_archive( 'course' ) ) {
$plugin_template = PLUGIN_DIR . 'templates/archive-course.php';
if ( file_exists( $plugin_template ) ) {
return $plugin_template;
}
}
return $template;
} );
- 在 archive-course.php 中复用主题 header/footer,同时注入 CPT 专属逻辑
<?php
/**
* Plugin Name: Course Archive Template
*/
get_header( 'course' ); // 自动调用 header-course.php,若不存在则降级 header.php
// 仅对当前 CPT 追加 body class
add_filter( 'body_class', function ( $classes ) {
if ( is_post_type_archive( 'course' ) ) {
$classes[] = 'archive-course';
}
return $classes;
} );
?>
<main id="main" class="site-main archive-course__grid">
<?php if ( have_posts() ) : ?>
<header class="archive-header">
<h1 class="archive-title"><?php post_type_archive_title(); ?></h1>
<p>共 <?= esc_html( $wp_query->found_posts ) ?> 门课程</p>
</header>
<?php while ( have_posts() ) : the_post(); ?>
<article <?php post_class( 'course-card' ); ?>>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'medium' ); ?>
<h2><?php the_title(); ?></h2>
</a>
</article>
<?php endwhile; ?>
<?php the_posts_pagination(); ?>
<?php else : ?>
<p>暂无课程</p>
<?php endif; ?>
</main>
<?php
get_footer( 'course' ); // 自动调用 footer-course.php,若不存在则降级 footer.php
- 若主题采用 block-based theme,则通过 theme.json 的 templateParts 注册 header-course,再用
<!-- wp:template-part {"slug":"header-course","theme":"child-theme"} /-->
在 archive-course.html 中引入,保证全站编辑兼容。
- 性能与安全加固
- 在 pre_get_posts 中限制每页 12 条,禁止 WordPress 默认的
SQL_CALC_FOUND_ROWS:
add_action( 'pre_get_posts', function ( $q ) {
if ( ! is_admin() && $q->is_post_type_archive( 'course' ) && $q->is_main_query() ) {
$q->set( 'posts_per_page', 12 );
$q->set( 'no_found_rows', true ); // 如无需分页可关闭
}
} );
- header 内输出的动态关键词用 esc_attr 包裹,防止 XSS。
- 对 header 中引入的 hero 图片使用 wp_get_attachment_image_srcset 提供响应式尺寸,减少移动端带宽。
- 单元测试
使用 WP_UnitTestCase 创建 20 条 course 记录,断言访问/courses/时:
- 状态码 200;
- 响应体包含
<h1>课程</h1>; - 包含 12 条
article.course-card; - 缓存头
X-Cache: HIT出现(验证 Nginx fastcgi_cache 生效)。
拓展思考
- 如果客户要求 CPT 归档与博客首页共用同一套“卡片”UI,但卡片内部字段不同,你会用 get_template_part( 'template-parts/card', get_post_type() ) 实现组件级复用,还是通过 render_block 把卡片做成动态区块?
- 当站点使用 headless WordPress + Next.js,前端路由
/courses不再经过 PHP,你如何保证 SEO 的 title、meta description、og:image 与后端 CPT 归档保持一致? - 在 多语言插件(WPML/Polylang) 场景下,CPT 的
has_archive设为 false 时,翻译后的归档页 404,你如何通过 filter rewrite_rules_array 动态注入翻译规则? - 如果 header 内需要展示 实时报名人数,而报名人数存储在 custom table 中,你会把查询放在 template_redirect 还是 wp_enqueue_scripts?如何防止未缓存的 SQL 拖垮首页?
- 当主题升级引入 全站 jQuery 3.x 而你的 CPT 归档脚本依赖 jQuery 1.12 的
.live()方法时,如何用 wp_add_inline_script 做 polyfill,同时保证 Core Web Vitals 的 TBT 不超标?
把这些问题提前准备成 STAR 案例 写进简历,面试时主动抛出,就能从“会用 header/footer”跃迁到“能扛大型商业项目”。