get_template_part( $slug, $name = null );
.php),可含子目錄,如 'part/loop' 對應 part/loop.php。{$slug}-{$name}.php。如 get_template_part('loop', 'product') → loop-product.php。<?php get_template_part('content'); ?> <!-- content.php -->
<?php get_template_part('part/loop'); ?> <!-- part/loop.php -->
<?php get_template_part('content', 'page'); ?> <!-- content-page.php -->
<?php get_template_part('content', get_post_format()); ?> <!-- 根據文章格式載入 content-video.php 等 -->
index.php、archive.php、search.php 均調用 get_template_part('content', get_post_format()),按文章格式渲染不同佈局。sidebar.php、nav-top.php,靈活組合。| 特性 | get_template_part | include/require |
|---|---|---|
| 子主題覆蓋 | 自動支持 | 需手動判斷 locate_template() |
| 文件不存在 | 靜默忽略 | 報警告/致命錯誤 |
| 命名約定 | {slug}-{name}.php | 任意 |
| 變量作用域 | 繼承調用處作用域 | 同 |
**結論**:主題開發**首選** get_template_part(),符合 WP 模板層級機制,利於維護與擴展。