get_template_part

函式
get_template_part ( $slug, $name = null, $args = array() )
引數
  • (string) $slug The slug name for the generic template.
    Required:
  • (string) $name The name of the specialised template.
    Required:
    Default: null
  • (array) $args Optional. Additional arguments passed to the template. Default empty array.
    Required:
    Default: array()
返回值
  • (void|false) Void on success, false if the template does not exist.
定義位置
相關方法
get_templateblock_template_partget_template_hierarchyget_template_directory_get_block_templates_paths
引入
3.0.0
棄用
-

get_template_part: 這個函式檢索一個模板部分檔案的內容。它需要兩個引數:模板部分的檔名,和一個可選的當前主題目錄下的子目錄名稱。它以字串形式返回模板部分檔案的內容。

將一個模板部分載入到一個模板中。

為子主題提供了一個簡單的機制,以便在主題中過載可重用的程式碼部分。

包括一個主題的命名的模板部分,或者如果指定了一個名稱,那麼一個特殊的部分將被包括。如果主題不包含{slug}.php檔案,那麼將不包含模板。

模板是使用require而不是require_once來包含的,所以你可以多次包含同一個模板部分。

對於$name引數,如果檔案被稱為”{slug}-special.php”,那麼指定”special”。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_template_part( $slug, $name = null, $args = array() ) {
/**
* 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
* @since 5.5.0 The `$args` parameter was added.
*
* @param string $slug The slug name for the generic template.
* @param string|null $name The name of the specialized template.
* @param array $args Additional arguments passed to the template.
*/
do_action( "get_template_part_{$slug}", $slug, $name, $args );
$templates = array();
$name = (string) $name;
if ( '' !== $name ) {
$templates[] = "{$slug}-{$name}.php";
}
$templates[] = "{$slug}.php";
/**
* Fires before an attempt is made to locate and load a template part.
*
* @since 5.2.0
* @since 5.5.0 The `$args` parameter was added.
*
* @param string $slug The slug name for the generic template.
* @param string $name The name of the specialized template.
* @param string[] $templates Array of template files to search for, in order.
* @param array $args Additional arguments passed to the template.
*/
do_action( 'get_template_part', $slug, $name, $templates, $args );
if ( ! locate_template( $templates, true, false, $args ) ) {
return false;
}
}
function get_template_part( $slug, $name = null, $args = array() ) { /** * 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 * @since 5.5.0 The `$args` parameter was added. * * @param string $slug The slug name for the generic template. * @param string|null $name The name of the specialized template. * @param array $args Additional arguments passed to the template. */ do_action( "get_template_part_{$slug}", $slug, $name, $args ); $templates = array(); $name = (string) $name; if ( '' !== $name ) { $templates[] = "{$slug}-{$name}.php"; } $templates[] = "{$slug}.php"; /** * Fires before an attempt is made to locate and load a template part. * * @since 5.2.0 * @since 5.5.0 The `$args` parameter was added. * * @param string $slug The slug name for the generic template. * @param string $name The name of the specialized template. * @param string[] $templates Array of template files to search for, in order. * @param array $args Additional arguments passed to the template. */ do_action( 'get_template_part', $slug, $name, $templates, $args ); if ( ! locate_template( $templates, true, false, $args ) ) { return false; } }
function get_template_part( $slug, $name = null, $args = array() ) {
	/**
	 * 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
	 * @since 5.5.0 The `$args` parameter was added.
	 *
	 * @param string      $slug The slug name for the generic template.
	 * @param string|null $name The name of the specialized template.
	 * @param array       $args Additional arguments passed to the template.
	 */
	do_action( "get_template_part_{$slug}", $slug, $name, $args );

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

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

	/**
	 * Fires before an attempt is made to locate and load a template part.
	 *
	 * @since 5.2.0
	 * @since 5.5.0 The `$args` parameter was added.
	 *
	 * @param string   $slug      The slug name for the generic template.
	 * @param string   $name      The name of the specialized template.
	 * @param string[] $templates Array of template files to search for, in order.
	 * @param array    $args      Additional arguments passed to the template.
	 */
	do_action( 'get_template_part', $slug, $name, $templates, $args );

	if ( ! locate_template( $templates, true, false, $args ) ) {
		return false;
	}
}

常見問題

FAQs
檢視更多 >