模板层次结构:与index.php,front-page.php,home.php混淆
-
-
@rarst与此:https://www.rarst.net/wordpress/front-page-logic/.可以帮助您.Got @rarst with this: https://www.rarst.net/wordpress/front-page-logic/. May help you.
- 1
- 2016-05-17
- Mayeenul Islam
-
6 个回答
- 投票数
-
- 2016-08-26
首页逻辑是WordPress中最令人困惑的功能之一,异常难以解释和总结.就像评论中提到的那样,我花了很多时间来整理我的首页逻辑作弊技巧表.
但是,由于这是一个受欢迎的话题,让我尝试回答您遇到的那些非常具体的问题.
home.php
和index.php
有什么区别?home.php
是帖子索引的模板(本机Post帖子类型的存档,这是WP中的一种特殊情况). WP会尝试查找帖子索引,无论它们显示在网站的根目录还是专用的帖子页面.index.php
是适用于所有模板的模板.这是模板层次结构所有分支中的最终选择,并且对于其他存档和单个视图,在没有其他合适条件时将被选择.只有帖子索引可以使用
home.php
,但是所有其他上下文可以并且将使用index.php
.使用
的理想条件是什么?home.php
而不是index.php
您使用
home.php
自定义帖子索引.您使用
index.php
提供主题中最通用的模板,适合于任何显示.某些主题选择具有空的
index.php
并确保它们在所有可能的情况下都具有更具体的模板,因此不必使用它.使用
front-page.php
的理想条件是什么?front-page.php
用于在根首页或静态首页(如果启用)上的帖子索引.这是一个高优先级模板,因此,如果有主题,则不能为静态首页选择任意模板.因此,它几乎从未包含在公开发布的主题中(正确).
它的最佳用途是在私有项目中,因为它比页面模板更容易配置.
当我使用
front-page.php
时,那么index.php
为我执行的具体任务是什么?index.php
仍然 捕获所有其他情况下的所有模板.如果您使用静态首页(
front-page.php
将应用到该首页),则您的帖子页面将尝试先使用home.php
,然后使用index.php
.Front page logic is one of the most confusing features in WordPress and is exceptionally hard to explain and summarize. As mentioned in comment while back I burnt unholy amount of time to put together my front page logic cheat sheet for it.
But since this is a popular thread let me try to answer those very specific questions you had.
What's the difference between the
home.php
andindex.php
?home.php
is template for posts index (archive of native Post post type, which is a special case in WP). WP will attempt to look it up for index of posts, whether they are displayed at the root of the site or at dedicated posts page.index.php
is catch–all template. It is final choices in all branches of template hierarchy and will be picked when nothing else fits, for both archives and singular views.Only posts index can use
home.php
, but all other contexts might and will useindex.php
.What's the ideal condition to use a
home.php
than anindex.php
You use
home.php
to customize posts index.You use
index.php
to provide the most generic template in your theme, suitable for display of anything.Some themes choose to have empty
index.php
and ensure they have more specific templates for all possible cases, so it never has to be used.What's the ideal condition to use a
front-page.php
?front-page.php
is used for posts index at the root or static front page, if enabled.It is a high priority template, so if theme has it you cannot select arbitrary template for static front page. For this reason it is almost never included in publicly released themes (which is correct).
The best use for it is in private projects, since it's easier to configure than page template.
When I'm using a
front-page.php
then what specific task anindex.php
is doing for me then?index.php
is still a catch all template for all other cases.If you use static front page (to which
front-page.php
will apply) then your posts page will try to usehome.php
and thenindex.php
. -
- 2013-08-16
front-page.php
文件是网站首页模板.无论将get_option( 'show_on_front' )
设置为page
还是posts
,它将始终在您的网站首页上使用.>home.php
模板文件是博客文章索引模板.无论博客文章索引显示在网站首页还是其他页面上,它始终将用于显示博客文章索引.如果
front-page.php
和home.hpp
同时存在,并且设置了get_option( 'show_on_front' )
到posts
(即网站首页显示博客文章索引),front-page.php
文件将优先于home.php
文件.对于中的所有上下文,
index.php
文件是默认后备模板模板层次结构.仅当当前上下文中不存在更特定的模板文件时,才使用该文件.网站首页的模板层次结构为:
-
front-page.php
- 如果
'page' == get_option( 'show_on_front' )
:页面层次结构 - 如果
'posts' == get_option( 'show_on_front' )
:博客文章索引层次结构
博客文章索引的模板层次结构为:
-
home.php
-
index.php
页面的模板层次结构为:
- 自定义页面模板
-
page-{slug}.php
-
page-{id}.php
-
page.php
-
index.php
The
front-page.php
file is the site front page template. It will always be used on your site front page, regardless of whetherget_option( 'show_on_front' )
is set topage
orposts
.The
home.php
template file is the blog posts index template. It will always be used to display your blog posts index, regardless of whether the blog posts index is displayed on the site front page, or on a different page.In the case that both
front-page.php
andhome.hpp
exist, and theget_option( 'show_on_front' )
is set toposts
(i.e., the site front page displays the blog posts index), thefront-page.php
file will take precedence over thehome.php
file.The
index.php
file is the default fallback template for all contexts in the Template Hierarchy. It is only ever used if a more-specific template file does not exist for the current context.The template hierarchy for the site front page is:
front-page.php
- If
'page' == get_option( 'show_on_front' )
: page hierarchy - If
'posts' == get_option( 'show_on_front' )
: blog posts index hierarchy
The template hierarchy for the blog posts index is:
home.php
index.php
The template hierarchy for pages is:
- Custom page template
page-{slug}.php
page-{id}.php
page.php
index.php
-
因此,如果我做对了,当首页设置为最新帖子时,不能使用"my-template-1"模板,而当首页设置为静态页面时,则不能使用"my-template-2"(无编码)?So, if i'm getting this right, it's not possible to use `my-template-1` template when the front page is set to latest posts and use `my-template-2` when the front is set to an static page (without coding) ?
- 0
- 2017-05-30
- Jack Johansson
-
- 2013-08-16
所有这些都来自对模板层次结构的透彻阅读.
在以下情况下使用home.php
:- 您网站的首页设置为显示帖子列表,或者
- 已设置静态首页,并且
- 访问者转到您定义为"帖子列表"的页面(例如,
http://example.com/blog/
)
- 主题有一个
home.php
文件
如果不满足最后一个条件-即主题中没有
如果出现以下情况,将使用home.php
文件-那么将加载index.php
.front-page.php
:- 已设置静态首页,并且
- 访问者转到您定义为"静态首页"的页面(即
http://example.com/
),并且 - 主题有一个
front-page.php
文件
如果不满足最后一个条件-即主题中没有
front-page.php
文件-那么将加载index.php
总结
如果要自定义帖子列表,请使用
home.php
.
如果要自定义静态主页,请使用front-page.php
.All this comes from a thorough reading of the Template Hierarchy.
home.php
is used if:- Your site's front page is set to display a list of posts, or
- A static front page is set, and
- A visitor goes to the page that you've defined as your "list of posts" (eg,
http://example.com/blog/
)
- And the theme has a
home.php
file
If the last condition isn't met -- ie, there is no
home.php
file in the theme -- thenindex.php
will be loaded.front-page.php
will be used if:- A static front page is set, and
- A visitor goes to the page that you've defined as your "static home page" (ie,
http://example.com/
), and - The theme has a
front-page.php
file
If the last condition isn't met -- ie, there is no
front-page.php
file in the theme -- thenindex.php
will be loaded.In summary
If you want to customize your list of posts, use
home.php
.
If you want to customize your static home page, usefront-page.php
.-
无论位置如何," home.php"都将用于博客文章索引.如果首页设置为显示博客文章索引,则将使用" home.php"(如果"front-page.php"不存在).`home.php` will be used for the blog posts index, regardless of location. If the front page is set to display the blog posts index, then `home.php` will be used (if `front-page.php` does not exist).
- 2
- 2013-08-16
- Chip Bennett
-
- 2013-08-21
简而言之:
-
index.php
仅是一个后备模板,以防找不到合适的模板 -
home.php
用于博客(最新文章列表) -
front-page.php
用于登录页面
通用的
index.php
模板index.php
模板文件是后备模板.当没有其他合适的模板可用时,它将用作最后的选择.例如,如果您没有front-page.php
或home.php
,则将使用index.php
.对于缺少archive.php
或single.php
等的情况也是如此.关于
front-page.php
与VShome.php
现在
front-page.php
和home.php
模板之间的区别在于,使用了front-page.php
,例如它的名称表明,它是网站的主要首页,而home.php
旨在成为博客部分的主页.什么模板将用于我的网站的目标网页?
front-page.php
模板(如果存在)将用于网站的主页首页(例如http://www.example.com/
) .如果不存在,则将使用home.php
.如果同时缺少front-page.php
和home.php
模板文件,则将使用index.php
后备模板.>我的博客页面将使用什么模板?
WordPress允许您在与登录页面(例如
http://www.example.com/blog/
).因此,如果您的网站上是这种情况,那么Blog页面将始终使用home.php
模板.如果home.php
不存在,则将使用index.php
后备模板.如何配置登录页面和博客页面?
要配置要在首页上显示的内容,请转到 WordPress管理员,在工具>阅读下,您可以配置" 首页"显示"来显示静态页面或您的最新帖子.
如果您选择在首页上显示静态页面,则还可以设置将哪个页面用作Blog页面的占位符(它将使用
home.php
模板):In short:
index.php
is a fallback template only, in case no appropriate template was foundhome.php
is used for the blog (a listing of recent posts)front-page.php
is used for the landing-page
The universal
index.php
templateThe
index.php
template file is a fallback template. It is used as a last resort when no other more appropriate template is available. For example, if you don't have afront-page.php
nor ahome.php
thenindex.php
will be used. This is also true for missingarchive.php
orsingle.php
and so on.Regarding
front-page.php
VShome.php
Now the difference between
front-page.php
andhome.php
templates is that thefront-page.php
is used, as its name suggests, as the main front-page of the site, whilehome.php
is intended to be the home of the Blog section.What template will be used for my site's landing-page?
The
front-page.php
template, if present, will be used for the site's main front page (e.g.http://www.example.com/
). If not present, thenhome.php
will be used instead. If both thefront-page.php
andhome.php
template files are missing then theindex.php
fallback template will be used.What template will be used for my blog page?
WordPress allows you to have a "Blog" page (which will list recent posts) on a different page than the landing-page (e.g.
http://www.example.com/blog/
). So if this is the case on your site, then the Blog page will always use thehome.php
template. Ifhome.php
doesn't exist then theindex.php
fallback template will be used.How to configure the landing-page and blog page?
To configure what content to show on your front page, go to the WordPress Admin, under Tools > Reading, there you can configure the "Front page displays" to show a static page, or your latest posts.
In the case you choose to display a static page on the front page, then you also have the possibility to set which page to use as a placeholder for the Blog page (which will use the
home.php
template): -
- 2015-11-07
摘录自 WordPress模板层次结构
主页显示
默认情况下,WordPress将您网站的首页设置为显示您的 最新的博客文章.此页面称为博客文章索引.您可以 还可以将您的博客文章设置为显示在单独的静态页面上.的 模板文件home.php用于呈现博客文章索引,无论是否 它被用作首页或单独的静态页面.如果 home.php不存在,WordPress将使用index.php.
- home.php
- index.php
注意:如果存在front-page.php,它将覆盖home.php模板.
首页显示
front-page.php模板文件用于呈现网站的正面 页,首页是否显示博客文章索引(提到 以上)或静态页面.头版模板优先于 博客文章索引(home.php)模板.如果是front-page.php文件 不存在,WordPress将使用home.php或page.php 文件取决于设置→读取中的设置.如果两个都不 这些文件存在,它将使用index.php文件.
1.front-page.php –用于"您的最新帖子"或"静态页面" 如首页中设置的那样,显示"设置"→"阅读"部分.
2. home.php –如果WordPress无法找到front-page.php和"您的最新 帖子"设置在首页显示部分中,它将寻找 home.php.此外,WordPress会在 帖子页面设置在首页显示部分.
3.page.php –何时 在首页显示区域中设置"首页".
4.index.php – 在首页显示区域中设置"您的最新帖子"时, home.php不存在或设置首页时,但page.php确实存在 不存在.
如您所见,选择路径有很多规则 WordPress需要.使用上面的图表是确定的最佳方法 WordPress将显示什么.
taken from Wordpress template hierarchy
Home Page display
By default, WordPress sets your site’s home page to display your latest blog posts. This page is called the blog posts index. You can also set your blog posts to display on a separate static page. The template file home.php is used to render the blog posts index, whether it is being used as the front page or on separate static page. If home.php does not exist, WordPress will use index.php.
- home.php
- index.php
Note: If front-page.php exists, it will override the home.php template.
Front Page display
The front-page.php template file is used to render your site’s front page, whether the front page displays the blog posts index (mentioned above) or a static page. The front page template takes precedence over the blog posts index (home.php) template. If the front-page.php file does not exist, WordPress will either use the home.php or page.php files depending on the setup in Settings → Reading. If neither of those files exist, it will use the index.php file.
1. front-page.php – Used for both “your latest posts” or “a static page” as set in the front page displays section of Settings → Reading.
2. home.php – If WordPress cannot find front-page.php and “your latest posts” is set in the front page displays section, it will look for home.php. Additionally, WordPress will look for this file when the posts page is set in the front page displays section.
3. page.php – When “front page” is set in the front page displays section.
4. index.php – When “your latest posts” is set in the front page displays section but home.php does not exist or when front page is set but page.php does not exist.
As you can see, there are a lot of rules to what path WordPress takes. Using the chart above is the best way to determine what WordPress will display.
-
- 2017-04-25
设置各种首页显示选项后,我决定测试以下每个页面使用的模板.
- example.net
- example.net/home/
- example.net/blog/
结果相当长,但可以用作参考或备忘单.
来源: Front-page.php和home.php模板在WordPress中有何区别?
编辑者注意:我试图在此处复制并粘贴表格,但是需要markdown格式而不是HTML.如果可以,请转换为减价.
I decided to test which template is used for each of the following pages when the various Front page displays options are set.
- example.net
- example.net/home/
- example.net/blog/
The results are quite long, but can be used as a reference or cheat-sheet.
Source: How do the front-page.php and home.php templates differ in WordPress?
Note to editor: I tried to copy and paste the tables here but markdown format is required instead of HTML. Please convert to markdown if you can.
我真的对
index.php
,home.php
和front-page.php
感到困惑.在许多情况下,即使我有一个index.php
,我也会用front-page.php
覆盖它.但是最近,我知道了home.php
.home.php
和index.php
有什么区别?home.php
而不是index.php
的理想条件是什么?front-page.php
的理想条件是什么?front-page.php
时,那么index.php
的特定任务是什么呢?我已经阅读了Codex中的模板层次结构.请不要将我的问题与页面模板混淆,我理解他们,alHamduLILLAH.