WordPress模版引用模板文件完全指南

本文介绍了在WordPress主题中引用模板文件的多种方法,包括使用get_template_part()函数、包含子主题文件、直接使用include/require、条件包含、通过functions.php加载、短代码加载以及传递变量到模板。文章还提供了推荐的文件目录结构和最佳实践,强调get_template_part()为首选方法,因其支持子主题覆盖且符合WordPress标准。

文章作者:
阅读时间: 12 分钟
更新时间:2026年4月16日

在 WordPress 主题中,你可以通过以下几种方式引用模板文件:

1. 包含主题文件

使用 get_template_part()函数,这是最推荐的方法:

<?php get_template_part('content', 'single'); ?>
// 会按顺序查找:content-single.php → content.php

2. 包含子主题文件

如果你的主题支持子主题,使用 get_stylesheet_directory()

<?php get_template_part(get_stylesheet_directory() . '/template-parts/header'); ?>

3. 使用 include 或 require

直接包含文件路径:

<?php 
// 包含主题目录下的文件
include(get_template_directory() . '/template-parts/content.php');

// 或者使用 locate_template() 更安全
include(locate_template('template-parts/content.php'));
?>

4. 条件包含模板

根据条件包含不同的模板:

<?php
if (is_front_page()) {
    get_template_part('template-parts/content', 'frontpage');
} elseif (is_single()) {
    get_template_part('template-parts/content', 'single');
} else {
    get_template_part('template-parts/content');
}
?>

5. 在 functions.php 中加载模板文件

通过钩子加载:

add_action('wp_head', 'custom_template_part');
function custom_template_part() {
    if (is_single()) {
        get_template_part('template-parts/custom', 'header');
    }
}

6. 通过短代码加载

创建短代码来包含模板:

// functions.php
add_shortcode('my_template', 'load_my_template');
function load_my_template($atts) {
    ob_start();
    get_template_part('template-parts/my', 'template');
    return ob_get_clean();
}

// 在文章或页面中使用
// [my_template]

7. 主题文件结构示例

推荐的目录结构:

your-theme/
├── header.php
├── footer.php
├── index.php
├── single.php
├── page.php
└── template-parts/
    ├── content.php
    ├── content-single.php
    ├── content-page.php
    ├── header/
    │   ├── site-header.php
    │   └── site-navigation.php
    └── footer/
        ├── site-footer.php
        └── widgets.php

8. 传递变量到模板

如果需要传递变量:

<?php
$args = array(
    'title' => 'Hello World',
    'content' => 'This is some content'
);
set_query_var('template_args', $args);
get_template_part('template-parts/card', 'component');

// 在 template-parts/card-component.php 中
$args = get_query_var('template_args');
echo $args['title'];
?>

最佳实践建议:

  1. 使用 get_template_part()​ – 支持子主题覆盖
  2. 合理组织目录结构​ – 将模板部件放在 template-parts/目录
  3. 使用有意义的命名​ – 如 content-{post-type}.php
  4. 考虑性能​ – 避免在循环中多次包含大文件
  5. 遵循 WordPress 模板层级​ – 让 WordPress 自动选择最合适的模板

选择哪种方法取决于你的具体需求,但 get_template_part()通常是首选,因为它支持子主题覆盖并且更符合 WordPress 标准。

这篇文章有用吗?

点击星号为它评分!

平均评分 0 / 5. 投票数: 0

到目前为止还没有投票!成为第一位评论此文章。

在AI工具中继续讨论:
曾凤祥
曾凤祥
WordPress技术负责人
WordPress 独立站开发领域 10+ 年实践经验,长期专注于外贸独立站搭建与 SEO 优化,累计服务企业客户数百家(含制造业、外贸企业、政府等行业)
相关文章
无论你是否已有网站,我们都能帮你把线上业务推上新高度
无论什么行业,都能快速拥有专业网站:
无论什么行业,都能快速拥有专业网站:

展示型官网 / 品牌站 / 外贸独立站,均有成熟模板与定制方案
无需懂代码:可视化编辑+我们指导,轻松启动 → 快速上线,抢占先机​
结构清晰、利于SEO与后期运营,降低长期维护成本

立即查看建站方案
网站加载慢、跳出高、询盘少?
网站加载慢、跳出高、询盘少?

老旧体验与技术隐患会直接拖累获客与转化。
我们提供:网站全面诊断 → 速度/安全/结构优化 → 可持续运维支持(技术+策略),让网站真正成为您的业务增长工具,而不只是“线上门面”。

马上获取专属优化方案
微信联系
chat 扫码联系
模板建站
挑选模板
网站定制
免费诊断
咨询热线
咨询热线

189-0733-7671

返回顶部