返回介绍

get_template_part()

发布于 2017-09-11 00:20:09 字数 4878 浏览 1148 评论 0 收藏 0

get_template_part( string $slug,  string $name = null )

Load a template part into a template


description

Makes it easy for a theme to reuse sections of code in a easy to overload way for child themes.

Includes the named template part for a theme or if a name is specified then a specialised part will be included. If the theme contains no {slug}.php file then no template will be included.

The template is included using require, not require_once, so you may include the same template part multiple times.

For the $name parameter, if the file is called "{slug}-special.php" then specify "special".


参数

$slug

(string) (Required) The slug name for the generic template.

$name

(string) (Optional) The name of the specialised template.

Default value: null


源代码

File: wp-includes/general-template.php

function get_template_part( $slug, $name = null ) {
	/**
	 * Fires before the specified template part file is loaded.
	 *
	 * The dynamic portion of the hook name, `$slug`, refers to the slug name
	 * for the generic template part.
	 *
	 * @since 3.0.0
	 *
	 * @param string      $slug The slug name for the generic template.
	 * @param string|null $name The name of the specialized template.
	 */
	do_action( "get_template_part_{$slug}", $slug, $name );

	$templates = array();
	$name = (string) $name;
	if ( '' !== $name )
		$templates[] = "{$slug}-{$name}.php";

	$templates[] = "{$slug}.php";

	locate_template($templates, true, false);
}

更新日志

Versiondescription
3.0.0Introduced.

More Information

Usage


get_template_part( $slug );

get_template_part( $slug, $name );

Note: get_template_part() fails silently


相关函数

Uses

  • wp-includes/general-template.php: get_template_part_{$slug}
  • wp-includes/plugin.php: do_action()
  • wp-includes/template.php: locate_template()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 17You must log in to vote on the helpfulness of this note Contributed by Codex

    Using loop.php in child themes
    Assuming the theme folder is wp-content/themes, that the parent theme is twentyten, and the child theme is twentytenchild, then the following code —

    
    <?php get_template_part( 'loop', 'index' ); ?>
    

    will do a PHP require() for the first file that exists among these, in this priority:

    wp-content/themes/twentytenchild/loop-index.php
    wp-content/themes/twentyten/loop-index.php
    wp-content/themes/twentytenchild/loop.php
    wp-content/themes/twentyten/loop.php

  2. Using with theme subfolders

    To use this function with subfolders in your theme directory, simply prepend the folder name before the slug. For example, if you have a folder called “partials” in your theme directory and a template part called “content-page.php” in that sub-folder, you would use get_template_part() like this:

    
    <?php get_template_part( 'partials/content', 'page' ); ?>
    

    Navigation
    Adding a navigation bar to theme using a generic nav.php template file:

    
    <?php get_template_part( 'nav' );           // Navigation bar (nav.php) ?>
    <?php get_template_part( 'nav', '2' );      // Navigation bar

    Get a specific file

    Although this kind of defeats the purpose of this function, it’s also possible to load a specific template file with it. All you have to do is use just one argument:

    <?php get_template_part('template-parts/layout'); ?>

    will include layout.php from template-parts subdirectory placed in the root of your theme folder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文