
WordPress Template Part(模板片段)全面解析:原理、用法与实战
很多人刚接触 WordPress 主题开发时,会把所有代码都堆在:
single.phppage.phpindex.php
里面。
一开始没问题,但随着页面越来越复杂,很快就会出现:
- 重复代码越来越多
- 一个页面几千行
- 修改一个卡片,要改 5 个文件
- 无法复用组件
- 后期维护崩溃
这时候,WordPress 的 Template Part(模板片段) 就非常重要了。
它本质上:
就是 WordPress 的“组件化”。
你可以把:
- 文章卡片
- 面包屑
- 作者信息
- 广告位
- 历史上的今天
- 相关文章
- 文档导航
拆成独立模块,然后在任何地方调用。
这就是现代 WordPress 开发里最核心的思想之一。
一、Template Part 是什么?
最经典的函数:
get_template_part();
作用:
加载一个可复用的 PHP 模板片段。
例如:
get_template_part('template-parts/content');
它会自动寻找:
template-parts/content.php
然后插入当前页面。
这有点像:
- React 的组件
- Vue 的组件
- Docusaurus 的 MDX Component
只是 WordPress 用的是 PHP。
二、为什么 Template Part 很重要?
假设:
首页、分类页、搜索页
都需要显示文章卡片。
很多新手会这样:
<div class="post-card">
<h2><?php the_title(); ?></h2>
</div>
复制到:
- home.php
- archive.php
- search.php
结果:
后期改一个样式,要改三遍。
而 Template Part:
get_template_part('template-parts/post-card');
真正代码只写一次:
template-parts/post-card.php
这样:
- 可维护
- 可复用
- 更像现代前端开发
- 更适合大型网站
三、推荐目录结构
推荐这样组织:
theme/
├── style.css
├── functions.php
├── single.php
├── page.php
├── archive.php
├── template-parts/
│
├── components/
│ ├── post-card.php
│ ├── author-box.php
│ ├── toc.php
│ └── related-posts.php
│
├── sections/
│ ├── homepage-hero.php
│ └── homepage-featured.php
│
└── widgets/
├── today-history.php
└── ads-banner.php
这是大型 WordPress 项目常见写法。
四、最基础例子
1. 创建模板片段
// template-parts/post-card.php
?>
<article class="post-card">
<h2>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
<div class="excerpt">
<?php the_excerpt(); ?>
</div>
</article>
2. 在 archive.php 调用
<?php
while(have_posts()) :
the_post();
get_template_part('template-parts/post-card');
endwhile;
?>
这样:
所有文章列表统一使用同一个组件。
五、get_template_part 的工作原理
很多人以为它只是 include。
其实它还有:
- 模板查找机制
- 子主题覆盖机制
- 命名变体机制
例如:
get_template_part('template-parts/content', 'news');
WordPress 会优先寻找:
template-parts/content-news.php
找不到才会:
template-parts/content.php
这就是:
WordPress 模板层级思想。
六、实际开发中的高级用法
1. 不同文章类型加载不同模板
get_template_part(
'template-parts/content',
get_post_type()
);
如果文章类型是:
product
则自动加载:
template-parts/content-product.php
非常优雅。
2. 分类不同加载不同组件
if(in_category('news')) {
get_template_part(
'template-parts/card',
'news'
);
} else {
get_template_part(
'template-parts/card',
'default'
);
}
七、WordPress 5.5+ 新特性:传参
以前:
Template Part 最大的问题:
无法传变量。
WordPress 5.5 开始:
支持:
get_template_part(
'template-parts/banner',
null,
[
'title' => 'Hello',
'desc' => 'World'
]
);
然后在:
banner.php
里面:
echo $args['title'];
echo $args['desc'];
这已经非常接近现代组件开发了。
八、实战案例:历史上的今天组件
这是很典型的组件化思路。
1. 创建组件
// template-parts/today-history.php
$date = date('m-d');
$events = [
'05-28' => '某历史事件',
'06-01' => '儿童节'
];
?>
<div class="today-history">
<h3>历史上的今天</h3>
<p>
<?php
echo $events[$date] ?? '暂无内容';
?>
</p>
</div>
2. 页面调用
get_template_part(
'template-parts/today-history'
);
这样:
- 首页能用
- 文章页能用
- 侧边栏能用
完全模块化。
九、Template Part 与 include 的区别
很多人会问:
为什么不用:
include 'xxx.php';
原因:
1. 支持子主题覆盖
WordPress 会:
- 先找子主题
- 再找父主题
这是 include 做不到的。
2. 更符合 WordPress 生态
很多:
- Astra
- GeneratePress
- Kadence
主题都大量使用:
get_template_part()
3. 可维护性更高
大型项目:
组件化是必须的。
否则后期会越来越乱。
十、Template Part 的核心思想
其实:
Template Part 本质上:
就是在 WordPress 里实现“组件化开发”。
这是 WordPress 从传统 PHP 网站:
逐渐现代化的重要一步。
现在:
- Gutenberg
- Block Theme
- Full Site Editing
本质上都在继续强化:
“组件化”
这个方向。
十一、Template Part 的最佳实践
推荐:
小组件化
不要:
一个 template part 写 1000 行
而是:
文章卡片
作者信息
上一篇下一篇
目录
广告
推荐阅读
全部拆开。
不要写业务逻辑
Template Part:
最好负责:
展示
不要:
数据库查询
复杂业务
命名统一
推荐:
template-parts/
components/
sections/
partials/
统一一种。
十二、现代 WordPress 的开发趋势
现在大型 WordPress 项目:
越来越像:
- React
- Vue
- Next.js
因为:
大家都在:
- 组件化
- 模块化
- 解耦
Template Part:
其实就是 WordPress 时代最经典的组件方案。
即使未来:
- Gutenberg
- Block Theme
越来越流行,
Template Part 的思想仍然不会过时。
因为:
组件化永远不会过时。
总结
Template Part 最大价值:
- 减少重复代码
- 提高维护性
- 组件化开发
- 更适合大型站点
- 更接近现代前端思想
核心函数:
get_template_part()
建议:
从小项目开始,就养成组件化习惯。
否则:
后面代码会越来越难维护。
特别是:
- 文档站
- 企业站
- 内容平台
- CMS系统
Template Part 几乎是必备技能。
