当前浏览:首页 / WordPress函数 / get_template_part()

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

将模板片段加载到模板中

part

templatemore...


描述

为子主题提供了一种简单的机制,用于重载主题中可重用的代码段。

加载主题的命名模板片段,或者如果指定了名称,则将加载专用片段。如果主题不包含{slug}.php文件,则不会加载任何模板。

引入模板使用require,而不是require_once,因此您可以多次引入同一模板片段。

对于$name参数,如果文件名为“{slug}-special.php”,则指定“special”。


参数

$slug

(string) (必填) 通用模板的slug名称。

$name

(string) (可选) 指定模板的名称。

默认值: null

$args

(array) (可选) 传递给模板的其他参数。

默认值: array()


返回

(void|false) 成功时无返回,如果模板不存在,则为false。


说明

用法

get_template_part( $slug );
get_template_part( $slug, $name );

注意:get_template_part()失败无提示



源码

查看源码 官方文档


更新日志

版本描述
5.5.0添加了$args参数
3.0.0开始引入

使用示例

  • 示例1

    与主题子文件夹一起使用

    要对主题目录中的子文件夹使用此函数,只需在slug之前加上文件夹名。例如,如果您在主题目录中有一个名为“partials”的文件夹,在该子文件夹中有一个名为“content-page.php”的模板片段,您将使用如下get_template_part()

    <?php get_template_part( 'partials/content', 'page' ); ?>
    
  • 示例2

    在WordPress 5.5更新后传递变量
    ======================================

    调用函数时

    get_template_part(
    	'template-part',
    	'name',
    	array(
    		'key'	=> 'value',
    		'key2'	=> 'value2'
    	)
    );

    在模板片段中

    var_dump( $args );	// Everything
    echo $args['key'];	// Specific values
  • 示例3

    旧codex中有一条关于“将变量传递给模板”的条目

    因为模板是必需的,所以它将无法访问您在调用主题的PHP代码中定义的任何变量,除非您显式地将它们声明为全局变量。

    但是,load_template()由get_template_part()间接调用,它将所有WP_Query变量提取到加载的模板的范围中。因此,可以使用set_query_var()使变量可用于模板片段。

    // You wish to make $my_var available to the template part at `content-part.php`
    set_query_var( 'my_var', $my_var );
    get_template_part( 'content', 'part' );
    
  • 示例4

    子主题使用loop.php
    假设主题文件夹是wp-content/themes,父主题是twentyten,子主题是twentytenchild,那么下面的代码 —

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

    将对其中存在的第一个文件执行PHPrequire(),优先级如下:

    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

  • 示例5

    导航使用通用nav.php模板文件向主题添加导航栏:

    <?php get_template_part( 'nav' );           // Navigation bar (nav.php) ?>
    <?php get_template_part( 'nav', '2' );      // Navigation bar #2 (nav-2.php) ?>
    <?php get_template_part( 'nav', 'single' ); // Navigation bar to use in single pages (nav-single.php) ?>
    
  • 示例6

    关于如何在模板片段中使用WordPress 5.5中的$args参数的简单示例。

    <?php 
    
    $args = array( 'section_title' => 'hello world' );
    
    get_template_part( 'template-parts/file', 'name', $args ); // template-parts/file-name.php
    
    

    在模板片段

    <h2><?php echo __( $args['section_title'] ); ?></h2>

    输出:

    <h2>hello world</h2>
  • 示例7

    2019年1月以来,有时如果没有找到模板文件,该函数已更改为返回false。代码注释或变更日志中没有注意到这一点。注意,函数在静默状态下失败不再正确。

    这使得插件可以很容易地提供如下模板回退(或允许模板覆盖,取决于您的视角):

    if ( false === get_template_part( $slug, $name, $args ) ) {
    	// default output
    }
    
  • 示例8

    自WP 5.5,$args参数用于通过模板传递变量。

    <?php
    
    $args = ['some data', 'to use'];
    get_template_part( 'content', '', $args ); ?>
    
    /* content.php */
    <?php print_r($args); ?>
    
  • 示例9

    获取特定文件

    虽然这种方法有损于此函数的用途,但也可以使用它加载特定的模板文件。您只需使用一个参数:

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

    将引入位于主题文件夹根目录中的template-parts子目录中的layout.php

  • 示例10

    在$args中使用布尔值

    <?php get_template_part( 
        'template-parts/blocks/inc/content', 
        'section-header', 
        array( 'hasBtn' => true )
    ); ?>
    
    <?php if ( ! empty( $args['hasBtn'] ) ) { ?>
            //show the btn on the template part
    <?php } ?>
    
  • 示例11

    您还可以通过以下操作访问模板片段文件中的模板变量:

    extract( $args );
    
    echo $variable1;
    echo $variable2;
    

    您的get_template_part看起来像这样:

    $args = array(
    	'variable1' => '$variable1 Value',
    	'variable2' => '$variable2 Value',
    );
    
    get_template_part( 'template-parts/template-part-file', null, $args );
    

    –请记住,第三个参数$args是从5.5版开始添加的

  • 示例12

    get_template_part(foldername/filename without .php extension)

    <?php
    if(is_user_logged_in()){
    	get_template_part('template-parts/loginuser');
    }
    else{
    	get_template_part('template-parts/nonloginuser');
    }
    ?>
    

    在主题主目录中创建文件夹

    文件夹名为“template-parts”,然后在“template-parts”文件夹中添加两个 .php 文件 loginuser.php 和 nonloginuser.php

    如果用户登录,则显示loginuser.php文件内容

    如果用户未登录,则显示nonloginuser.php文件内容

    注意:上面使用的文件夹和文件名可以任意,我是随便起的

  • 示例13

    如果您的web服务器正在使用ModSecurity,它很可能会阻止使用类似file_get_contents()的PHP方法内联SVG的尝试。您可以使用get_template_part()内联SVG。

    创建你的SVG副本,其中icon.svg 变成 inline-icon.svg.php
    在你的模板中内联使用它们 get_template_part( 'images/inline', 'icon.svg' );

    在编写本文时,这可以绕过ModSecurity的默认OWASP规则集。未使用其他规则集进行测试。