wordpress模板

tech2023-01-06  113

wordpress模板

WordPress themes allow you to change the design of your website, providing control over the look and feel. They are essentially collections of different template files that work together and produce the overall design of your website.

WordPress主题允许您更改网站的设计,从而控制外观。 它们本质上是不同模板文件的集合,这些文件一起工作并产生网站的总体设计。

Before you dive deeper into WordPress themes, it is important that you have an understanding of how WordPress operates and how the different pages of your website are created. WordPress interacts with a database to store your content. That means whenever you create a new post or page, the content of your post or page lives in a database and it’s only displayed using templates when the visitor asks for that particular content.

在深入研究WordPress主题之前,重要的是要了解WordPress的操作方式以及网站的不同页面的创建方式。 WordPress与数据库进行交互以存储您的内容。 这意味着每当您创建新的帖子或页面时,帖子或页面的内容就会保存在数据库中,并且仅在访问者要求特定内容时才使用模板显示。

A WordPress theme is a collection of templates (CSS and PHP files) that WordPress uses to display the database content. WordPress will look for files in a theme to use to render the current page in a specific order. This is called a ‘template hierarchy’, that is used to create and extend WordPress themes.

WordPress主题是WordPress用于显示数据库内容的模板(CSS和PHP文件)的集合。 WordPress将在主题中查找文件,以按特定顺序呈现当前页面。 这称为“模板层次结构”,用于创建和扩展WordPress主题。

WordPress themes decide how your content should be displayed on your website. A template in WordPress is a single file that determines what a specific page or set of pages look like on the front end of your website. When you change your theme, you are changing the way the content is displayed without changing the content itself.

WordPress主题决定如何在您的网站上显示您的内容。 WordPress中的模板是一个文件,用于确定网站前端上特定页面或页面集的外观。 更改主题时,将更改内容的显示方式,而不更改内容本身。

WordPress themes use different templates files to generate a complete web page. For example, when you access a WordPress website, you can actually see multiple different template files combined together to create one complete page.

WordPress主题使用不同的模板文件来生成完整的网页。 例如,当您访问WordPress网站时,实际上可以看到将多个不同的模板文件组合在一起以创建一个完整的页面。

For instance, if you will look at an index page, like the front page of a blog, you will usually see a header created by the header.php template, content created by index.php, sidebar created by sidebar.php and the footer created by footer.php

举例来说,如果你看一个索引页,就像一个博客的头版,你通常会看到由创建一个头header.php模板,内容所创造index.php ,侧边栏所创造sidebar.php和页脚由footer.php创建

.

However, if you access a single page, you will still see the same header, same sidebar and same footer but the content would be created by single.php instead. The question is how do you know which of these template files kick in and at what point? That’s where the WordPress template hierarchy comes in.

但是,如果您访问单个页面,您仍然会看到相同的标题,相同的侧边栏和相同的页脚,但是内容将由single.php创建。 问题是您如何知道这些模板文件中的哪些文件在何时启动? 这就是WordPress模板层次结构出现的地方。

The WordPress template hierarchy determines in what order your template files are loaded within your WordPress theme. WordPress searches for these files, to determine how to load your site.

WordPress模板层次结构确定在WordPress主题中以什么顺序加载模板文件。 WordPress搜索这些文件,以确定如何加载您的网站。

If you open the ‘Twenty Fifteen’ (one of the default WordPress themes) theme folder, there are more than 15 templates files. These include template files for 404, archive, author-bio, comments and content-link just to name a few. WordPress will use the Query String information contained within each link on your website to decide which template or set of templates will be used to display the page.

如果您打开“二十十五”(默认的WordPress主题之一)主题文件夹,则将有超过15个模板文件。 其中包括404,档案,作者简介,评论和内容链接的模板文件,仅举几例。 WordPress将使用网站上每个链接中包含的查询字符串信息来确定将使用哪个模板或一组模板来显示页面。

WordPress decides what type of page (search page, category page or home page for example) is being requested and then chooses the appropriate template suggested by the WordPress Template hierarchy to generate the content.

WordPress决定要请求的页面类型(例如搜索页面,类别页面或主页),然后选择WordPress模板层次结构建议的适当模板来生成内容。

命名约定 (Naming Convention)

WordPress has a special and strict naming convention associated with theme files, so all themes share a fairly commonly named set of files in the back end. For example, to easily manage your content you can create header.php and footer.php files. These template files must be named header.php and footer.php.

WordPress具有与主题文件相关的特殊且严格的命名约定,因此所有主题在后端共享一组相当通用的文件。 例如,要轻松管理内容,可以创建header.php和footer.php文件。 这些模板文件必须命名为header.php和footer.php 。

WordPress looks for template files with specific names in the current theme’s directory to determine which template to use, most commonly the first matching template file is used. If WordPress cannot find a template file with a matching name, it skips down to the next file name in the hierarchy. If WordPress fails to find any matching template file, it will use index.php (the theme’s home page template file).

WordPress在当前主题的目录中查找具有特定名称的模板文件,以确定要使用的模板,最常见的是使用第一个匹配的模板文件。 如果WordPress无法找到具有匹配名称的模板文件,它将跳至层次结构中的下一个文件名。 如果WordPress无法找到任何匹配的模板文件,它将使用index.php(主题的主页模板文件)。

So when you are editing template files what you have to think about is what type of content this is, then edit the appropriate template file for that type of content. The same applies to when you’re creating WordPress themes from scratch.

因此,当您编辑模板文件时,您需要考虑的是内容的类型,然后为该类型的内容编辑适当的模板文件。 从头开始创建WordPress主题时也是如此。

最低主题要求 (Minimum Theme Requirements)

The only files that are required for a WordPress theme to work are index.php and style.css. The index.php is the main and only file that will process every WordPress query, as well as the structure of the site.

WordPress主题正常工作所需的唯一文件是index.php和style.css 。 index.php是将处理每个WordPress查询以及站点结构的主要文件,也是唯一的文件。

The minimum files required for a WordPress theme to work and to be visible in the WordPress dashboard for activation are index.php and style.css.

WordPress主题正常工作并在WordPress仪表板中可见以进行激活所需的最小文件为index.php和style.css 。

If index.php or style.css are missing, you will see following error.

如果缺少index.php或style.css ,您将看到以下错误。

The following themes are installed but are incomplete. Themes must have a style-sheet and a template.

以下主题已安装,但不完整。 主题必须具有样式表和模板。

style.css (required)

style.css (必填)

index.php (required)

index.php (必需)

functions.php (optional)

functions.php (可选)

screenshot.png (optional)

screenshot.png (可选)

This separation greatly increases efficiency. Combining everything into one big single file index.php might technically work, but it wouldn’t be pretty, and would cause endless issues down the line. Also editing a theme would be a nightmare!

这种分离大大提高了效率。 从技术上讲,将所有内容组合到一个大的单个文件index.php可能会起作用,但是它不是很漂亮,并且会导致无休止的问题。 同时编辑主题将是一场噩梦!

Here is the list of the theme files recognized by WordPress.

这是WordPress可以识别的主题文件列表。

style.css

style.css

rtl.css

rtl.css

index.php

index.php

comments.php

comments.php

front-page.php

front-page.php

single.php

single.php

page.php

page.php

author.php

author.php

archive.php

archive.php

There are many other files as well, you can view the template files list for more details.

还有许多其他文件,您可以查看模板文件列表以获取更多详细信息。

二十五个模板层次结构示例 (Twenty Fifteen Template Hierarchy Example)

Suppose you have activated the Twenty Fifteen theme and a user visits the page yoursite.com/author/tahir/. First, WordPress will search for the template labeled author-tahir.php but author-tahir.php is not available. WordPress will then look for author.php, if it also does not exist, it will look for archive.php. This file is available in Twenty Fifteen theme. If you delete or rename archive.php then WordPress will use index.php to render the page.

假设您已经激活了“二十一十五”主题,并且用户访问了yoursite.com/author/tahir/页面。 首先,WordPress将搜索标有author-tahir.php的模板,但author-tahir.php不可用。 然后,WordPress将寻找author.php ,如果它也不存在,它将寻找archive.php 。 该文件有二十五个十五主题。 如果您删除或重命名archive.php则WordPress将使用index.php渲染页面。

Each time a user visits your website, WordPress moves up the template hierarchy until it finds a template file that matches. This has everything to do with how these files are named.

每次用户访问您的网站时,WordPress都会向上移动模板层次结构,直到找到匹配的模板文件。 这与这些文件的命名方式有关。

“文件”插件 (The ‘What The File’ Plugin)

To find out what file and template WordPress is using to display page content you are currently viewing, you can use the very handy What The File Plugin. This is a free plugin developed by Barry Kooij.

要找出WordPress用于显示当前正在查看的页面内容的文件和模板,可以使用非常方便的What The File Plugin 。 这是Barry Kooij开发的免费插件。

This plugin adds an option to your toolbar showing what file and templates are being used. I installed the ‘What The File Plugin’ for testing purposes on my local server and found it worked great.

此插件向您的工具栏添加一个选项,显示正在使用的文件和模板。 我在本地服务器上安装了“什么文件插件”以进行测试,发现它工作得很好。

To test which files WordPress uses if a specific file isn’t available, I renamed two files (in the Twenty Fifteen Theme). In the screenshot below you can see which files WordPress uses to display content.

为了测试特定文件不可用时WordPress使用的文件,我重命名了两个文件(在“二十五个主题”中)。 在下面的屏幕截图中,您可以查看WordPress用于显示内容的文件。

You can see the Twenty Fifteen theme uses the content-search.php file to display search results. If you delete or rename content-search.php, WordPress will use content.php to display search results instead.

您可以看到“二十十五”主题使用content-search.php文件显示搜索结果。 如果删除或重命名content-search.php ,WordPress将使用content.php来显示搜索结果。

The ‘What The File’ plugin also allows you to click the file name directly to edit it through the theme editor. This plugin requires WordPress 3.1 or higher, it worked fine with WordPress 4.1 as well.

“文件文件”插件还允许您直接单击文件名,以通过主题编辑器对其进行编辑。 此插件需要WordPress 3.1或更高版本,也可以与WordPress 4.1配合使用。

有用的资源 (Useful Resources)

If you’re interested in learning more about the WordPress template hierarchy and theme development in general, I’d recommend the following resources:

如果您有兴趣总体上了解有关WordPress模板层次结构和主题开发的更多信息,我建议您使用以下资源:

Guidelines for Naming WordPress Theme Folders and Files

WordPress主题文件夹和文件的命名准则

Template Files List

模板文件列表

Template Hierarchy WordPress Codex

模板层次结构WordPress Codex

Stepping into Templates

进入模板

Getting Started with WordPress Theme Development

WordPress主题开发入门

翻译自: https://www.sitepoint.com/the-wordpress-template-hierarchy/

wordpress模板

最新回复(0)