如何使用get_template_part()?
-
-
尝试看看http://codex.wordpress.org/Function_Reference/get_template_partTry look at http://codex.wordpress.org/Function_Reference/get_template_part
- 1
- 2011-02-21
- wow
-
3 个回答
- 投票数
-
- 2012-02-08
一些很好的入门答案.
基本上,
get_template_part()
允许主题开发人员设置模板文件的特定顺序.将其应用于CSS选择器时,应将其与特定性相似.在设计某些东西时,您要从最低限度的特异性开始,以便可以在需要个人注意的设计部分轻松地覆盖它.因此,例如,您正在设计博客的样式并创建一个 loop.php 文件,该文件非常适合标记帖子.但是您要事先计划,然后再在模板文件中使用其他上下文说明符来调用它-例如,在索引页面上,您可以在单个页面上调用
get_template_part( 'loop', 'index' );
模板,您调用get_template_part( 'loop', 'single' );
,在存档页面上,您调用get_template_part( 'loop', 'archive' );
,依此类推上.当您决定在存档页面上标记与首页不同的标记时,这非常容易:只创建一个 loop-archive.php 模板即可使用,而不是使用通用 loop.php .但是
get_template_part()
背后的魔力在于函数locate_template()
,该函数首先检查主题目录,然后检查父目录(如果存在).文件名为.这对于插件开发非常有用.在我的一个插件中,我定义了一个自定义帖子类型,并在我的插件目录中为此自定义帖子类型创建了一个循环模板文件.但是...如果他们愿意,我想允许使用插件的主题覆盖我的标记.这是locate_template()
真正起作用的地方.locate_template($template_names, $load = false, $require_once = true )
将在样式表目录的$template_names数组中,然后在模板目录中查找每个名称.传递"true"作为$ load参数意味着它将需要找到的第一个文件,如果未找到模板文件,则将返回一个空字符串.因此,我可以在插件中执行以下操作:
if ( '' === locate_template( 'loop-mycustomposttype.php', true, false ) ) include( 'loop-mycustomposttype.php' );
...希望这可以使主题开发人员仅在主题中包含一个名为 loop-mycustomposttype.php 的文件即可轻松自定义我的插件.
Some very good introductory answers here.
Basically,
get_template_part()
allows theme developers to set up an order of specificity of template files. Think of it similarly to specificity as it applies to CSS selectors. When designing something, you want to start with the bare minimum of specificity, so that it can be easily overridden in parts of a design that need individual attention.So for example, you're styling a blog and you create a loop.php file which works well for marking up posts. But you plan ahead, and you call it in your template files later with additional context specifiers - say, on the index page, you call
get_template_part( 'loop', 'index' );
, on the single template, you callget_template_part( 'loop', 'single' );
, on archive pages, you callget_template_part( 'loop', 'archive' );
, and so on. This makes it very easy down the road when you decide to mark up the loop on your archive pages differently from the home page: just create a loop-archive.php template and it'll be used rather than the generic loop.php.But the magic behind
get_template_part()
is in the functionlocate_template()
, which checks first the theme directory, then the parent directory (if one exists) for the file named. This is very useful for plugin development. In one of my plugins, I define a custom post type and created a loop template file for that custom post type in my plugin directory. But... I want to allow themes using my plugin to override my markup if they choose. This is wherelocate_template()
really works wonders.locate_template($template_names, $load = false, $require_once = true )
will look for each of the names in the $template_names array in the stylesheet directory, then in the template directory. Passing 'true' as the $load argument means that it will require the first file found, and will return an empty string if no template file was located. So I can do something like this in my plugin:
if ( '' === locate_template( 'loop-mycustomposttype.php', true, false ) ) include( 'loop-mycustomposttype.php' );
...which should hopefully make it very easy for theme developers to customize my plugin just by including a file called loop-mycustomposttype.php in their theme.
-
请以此替换locate_template. `include(locate_template('loop-mycustomposttype.php'))` 这样可以传递变量. 我在[链接](http://keithdevon.com/passing-variables-to-get_template_part-in-wordpress/)中找到了它.这非常有用!Replace locate_template by this, please. `include(locate_template( 'loop-mycustomposttype.php'))` This way is possible to pass variables. I've found this here [link](http://keithdevon.com/passing-variables-to-get_template_part-in-wordpress/). It's extremely useful!
- 2
- 2014-01-09
- Pablo S G Pacheco
-
这样做也有必要更改它.像这样的`if(false===include(locate_template('loop-mycustomposttype.php')))`Doing so it's going to be necessary to change this if also. Like this `if ( false === include(locate_template( 'loop-mycustomposttype.php')) ) `
- 1
- 2014-01-09
- Pablo S G Pacheco
-
哦,好点.使用我给出的公式,`require`或`require_once`调用(来自locate_template)位于函数内部,因此无法访问当前作用域.Oh, good point. With the formula I gave, the `require` or `require_once` call (from `locate_template`) is inside a function and so doesn't have access to the current scope.
- 1
- 2014-01-10
- goldenapples
-
- 2011-02-21
不是所有循环,主循环. ;-)无论您查看首页还是类别,或者知道什么,总会有一个主循环.该主循环的内容由在模板被调用之前 所运行的查询确定.
loop.php模板仅运行循环中的项目并对其进行格式化.参见食典中的文档.
如果查看二十一十的loop.php,您会发现二十一十然后使该单个模板文件中的多样化.
get_template_part()
仅加载并运行模板部分.您也可以将loop.php的一部分提取到单独的文件中,并用get_template_part('loop','category')
等调用代替.或者您可以为循环中的每个帖子提供一个部分模板,并使您的loop.php仅在
while内调用
子句.一切由您决定.get_template_part('loop','post');
...Not all loops, the main loop. ;-) No matter if you look at your frontpage or a category or whoknowswhat, you'll always have a main loop. The content of that main loop is determined by the query that's been run before your template got called at all.
The loop.php template merely runs over the items in the loop and formats them. See the documentation at the Codex.
If you look at Twenty-Ten's loop.php, you can see that Twenty-Ten then diversifies within that single template file.
get_template_part()
merely loads a template part and runs through it. You can just as well extract parts of your loop.php into separate files and replace them by aget_template_part('loop', 'category')
and so on calls.Or you could have a part-template for each individual post in the loop and have your loop.php only call
get_template_part('loop','post');
within thewhile...
clause. All up to you. -
- 2012-02-08
<?php get_template_part( 'loop', 'index' ); ?>
将对第一个存在的文件执行PHP require()...
如此有效地工作,就像您需要另一个php文件一样.
更新:"要求"略有不同-它包装在函数中,因此如果要将模板中的任何变量传递给函数,则必须
global
您的模板部分.From the get_template_part codex:
<?php get_template_part( 'loop', 'index' ); ?>
will do a PHP require() for the first file that exists...
So effectively it will work as if you were requiring another php file.
Update: There is a slight difference to 'require' - It is wrapped inside a function so you must
global
if you want to pass any variables from your template to your template part.
有人可以向我解释此功能的工作原理吗?我知道它的作用,但是当我查看二十个模板中的源代码时,我不知道如何在一个loop.php中收集所有循环(我也看到了该文件).
那么,例如,如何提取模板的某个公共部分,然后在其他模板之间重用呢?