💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
# 模板文件 ## 话题 * [模板术语](https://developer.wordpress.org/themes/basics/template-files/#template-terminology) * [模板文件](https://developer.wordpress.org/themes/basics/template-files/#template-files) * [模板部分](https://developer.wordpress.org/themes/basics/template-files/#template-partials) * [常见的WordPress模板文件](https://developer.wordpress.org/themes/basics/template-files/#common-wordpress-template-files) * [使用模板文件](https://developer.wordpress.org/themes/basics/template-files/#using-template-files) 模板文件用于整个WordPress主题,但首先让我们了解术语。 ## 模板术语[#模板术语](https://developer.wordpress.org/themes/basics/template-files/#template-terminology) 在处理WordPress主题时,术语“模板”以不同的方式使用: * 模板文件存在于主题中,并表示您的网站显示方式。 * [页面模板](https://developer.wordpress.org/themes/template-files-section/page-template-files/ "页面模板")是*仅 *适用 于页面以更改其外观的[模板](https://developer.wordpress.org/themes/template-files-section/page-template-files/ "Page Templates")。页面模板可以应用于单个页面,页面部分或一类页面。 * [模板标签](https://developer.wordpress.org/themes/basics/template-tags/ "模板标签")是WordPress的内置功能,您可以在模板文件中使用它来检索和显示数据(例如[`the_title()`](https://developer.wordpress.org/reference/hooks/the_title/ "功能参考/标题")和[`the_content()`](https://developer.wordpress.org/reference/hooks/the_content/ "功能参考/内容"))。 * [模板层次结构](https://developer.wordpress.org/themes/basics/template-hierarchy/ "模板层次结构")是WordPress根据所请求的内容来决定使用哪个主题模板文件的逻辑。 [顶部↑](https://developer.wordpress.org/themes/basics/template-files/#top) ## 模板文件[#模板文件](https://developer.wordpress.org/themes/basics/template-files/#template-files) WordPress主题由模板文件组成。这些是包含HTML,[模板标记](https://developer.wordpress.org/themes/basics/template-tags/ "模板标签")和PHP代码的混合的PHP文件。 构建主题时,将使用模板文件来影响网站不同部分的布局和设计。例如,您将使用`header.php`模板创建标题,或使用`comments.php`模板包含注释。 当有人访问您网站上的页面时,WordPress会根据请求加载模板。模板文件显示的内容类型由与模板文件关联的[发布类型](https://developer.wordpress.org/themes/basics/post-types/)确定。该 [模板层次](https://developer.wordpress.org/themes/basics/template-hierarchy/ "模板层次结构") 描述了模板文件的WordPress将加载基于请求的类型以及是否模板主题存在。然后,服务器解析模板中的PHP,并将HTML返回给访问者。 最关键的模板文件是`index.php`,如果在[模板层次结构中](https://developer.wordpress.org/themes/basics/template-hierarchy/)找不到更具体的模板,则该模板文件是全部模板。尽管主题仅需要一个 `index.php`模板,但是通常主题包含许多模板以显示不同的内容类型和上下文。 [顶部↑](https://developer.wordpress.org/themes/basics/template-files/#top) ## 模板谐音[#模板部分](https://developer.wordpress.org/themes/basics/template-files/#template-partials) 模板部分是模板的一部分,它作为另一个模板(例如网站标题)的一部分包括在内。模板部分可以嵌入多个模板中,从而简化主题创建。常见的模板部分包括: * `header.php`用于生成网站的标题 * `footer.php`用于生成页脚 * `sidebar.php`用于生成侧边栏 尽管上述模板文件在WordPress中是特殊情况,并且仅适用于页面的一部分,但是您可以创建任意数量的模板部分并将它们包括在其他模板文件中。 [顶部↑](https://developer.wordpress.org/themes/basics/template-files/#top) ## 常见的WordPress模板文件[#常见的WordPress模板文件](https://developer.wordpress.org/themes/basics/template-files/#common-wordpress-template-files) 以下是WordPress可以识别的一些基本主题模板和文件的列表。 **index.php** 主模板文件。在所有主题中都是**必需的**。 **style.css** 主要样式表。在所有主题中都是**必需的**,并且包含您主题的信息标题。 **rtl.css** 如果网站语言的文本方向是从右到左,则将自动包含从右到左的样式表。 **comments.php** 评论模板。 **front-page.php** 不管**管理>设置>阅读**上的什么设置,首页模板都始终用作网站首页(如果存在)。 **home.php** 主页模板默认为首页。如果未将WordPress设置为使用静态首页,则此模板用于显示最新帖子。 **header.php** 标头模板文件通常包含您站点的文档类型,元信息,样式表和脚本的链接以及其他数据。 **singular.php** 如果`single.php`找不到帖子,则使用单个模板;如果找不到页面,`page.php`则用于页面。如果`singular.php`找不到,`index.php`是 **single.php** 当访客请求单个帖子时,将使用单个帖子模板。 **single- {post-type} .php** 访客从自定义帖子类型请求单个帖子时使用的单个帖子模板。例如,`single-book.php`将用于显示来自名为*book*的自定义帖子类型的单个帖子。这`index.php` 如果不存在用于自定义帖子类型的特定查询模板,则使用此选项。 **archive- {post-type} .php** 当访问者请求自定义帖子类型存档时,将使用存档帖子类型模板。例如,`archive-books.php`将用于显示来自名为*books*的自定义帖子类型的帖子存档。< 如果`archive-{post-type}.php`没有,则使用archive.php模板文件。 **page.php** 当访客请求单个页面(内置模板)时,将使用页面模板。 **页面-{slug} .php** 访问者请求特定页面(例如带有“ about”块(page-about.php)的页面)时,将使用页面块模板。 **category.php** 当访客按类别请求帖子时,将使用类别模板。 **tag.php** 当访客按标签请求发布时,将使用标签模板。 **taxonomy.php** 当访客请求自定义分类法中的术语时,将使用分类法术语模板。 **author.php** 每当访问者加载作者页面时,都会使用作者页面模板。 **date.php** 当按日期或时间请求帖子时,将使用日期/时间模板。例如,使用这些条生成的页面: http://example.com/blog/2014/ http://example.com/blog/2014/05/ http://example.com/blog/2014/05/ 26 / **archive.php** 当访问者按类别,作者或日期请求帖子时,将使用存档模板。**注意**:如果存在更具体的模板(如`category.php`和)`author.php`,则此模板将被覆盖`date.php`。 **search.php** 搜索结果模板用于显示访问者的搜索结果。 **attachment.php** 当查看单个附件(例如图像,pdf或其他媒体文件)时,将使用附件模板。 **image.php** 图像附件模板是的更特定版本,`attachment.php`在查看单个图像附件时使用。如果不存在,则将使用WordPress`attachment.php`。 **404.php** 当WordPress无法找到符合访问者请求的帖子,页面或其他内容时,将使用404模板。 [顶部↑](https://developer.wordpress.org/themes/basics/template-files/#top) ## 使用模板文件[#使用模板文件](https://developer.wordpress.org/themes/basics/template-files/#using-template-files) 在WordPress模板中,您可以使用[模板标签](https://developer.wordpress.org/themes/basics/template-tags/ "模板标签")动态显示信息,包含其他模板文件或自定义网站。 例如,在您中,`index.php`您可以在最终生成的页面中包括其他文件: * 要包含标题,请使用[get\_header()](https://developer.wordpress.org/reference/functions/get_header/ "功能参考/获取标题") * 要包含侧边栏,请使用[get\_sidebar()](https://developer.wordpress.org/reference/functions/get_sidebar/ "功能参考/获取侧栏") * 要包含页脚,请使用[get\_footer()](https://developer.wordpress.org/reference/functions/get_footer/ "功能参考/获取页脚") * 要包含搜索表单,请使用[get\_search\_form()](https://developer.wordpress.org/reference/functions/get_search_form/ "功能参考/获取搜索表") * 要包含自定义主题文件,请使用[get\_template\_part()](https://developer.wordpress.org/reference/functions/get_template_part/ "功能参考/获取模板部分") 这是一个WordPress模板标签示例,可 *将*特定模板*包含*到您的页面中: 1个2个3&lt;?php get_sidebar(); ?&gt;&lt;?php get_template_part( 'featured-content' ); ?&gt;&lt;?php get_footer(); ?&gt; 您可以在“[模板标签”](https://developer.wordpress.org/themes/basics/template-tags/ "模板标签")上找到整页内容,以了解有关它们的全部信息。 有关链接组件模板的更多信息,请参考“[链接主题文件和目录](https://developer.wordpress.org/themes/basics/linking-theme-files-directories/ "链接主题文件和目录")”部分。