get_template_part() WordPress関数 インクルードタグ

get_template_part()

構文

get_template_part(①$slug)

get_template_part(①$slug,②$name)

参照:WordPress Codex-get template part

get_template_part()の詳細

 動 作 

モジュールテンプレートファイルを呼び出す。

 返り値 

なし

 引 数 

①(文字列)モジュールテンプレートのスラッグ名

②(文字列)(オプション)[デフォルト:なし]モジュールテンプレートの名前。

 

get_template_part()を使ったコード例

 実装 

<?php

get_template_part('content');

get_template_part('content','type1');

?>

content.phpを呼び出す。

次の行は、content-type1.phpを呼び出す。

関数内容

参照:wp-includes/general-template.php

<?php
114	/**
115	 * Loads a template part into a template.
116	 *
117	 * Provides a simple mechanism for child themes to overload reusable sections of code
118	 * in the theme.
119	 *
120	 * Includes the named template part for a theme or if a name is specified then a
121	 * specialised part will be included. If the theme contains no {slug}.php file
122	 * then no template will be included.
123	 *
124	 * The template is included using require, not require_once, so you may include the
125	 * same template part multiple times.
126	 *
127	 * For the $name parameter, if the file is called "{slug}-special.php" then specify
128	 * "special".
129	 *
130	 * @since 3.0.0
131	 *
132	 * @param string $slug The slug name for the generic template.
133	 * @param string $name The name of the specialised template.
134	 */
135	function get_template_part( $slug, $name = null ) {
136	        /**
137	         * Fires before the specified template part file is loaded.
138	         *
139	         * The dynamic portion of the hook name, `$slug`, refers to the slug name
140	         * for the generic template part.
141	         *
142	         * @since 3.0.0
143	         *
144	         * @param string      $slug The slug name for the generic template.
145	         * @param string|null $name The name of the specialized template.
146	         */
147	        do_action( "get_template_part_{$slug}", $slug, $name );
148	
149	        $templates = array();
150	        $name      = (string) $name;
151	        if ( '' !== $name ) {
152	                $templates[] = "{$slug}-{$name}.php";
153	        }
154	
155	        $templates[] = "{$slug}.php";
156	
157	        /**
158	         * Fires before a template part is loaded.
159	         *
160	         * @since 5.2.0
161	         *
162	         * @param string   $slug      The slug name for the generic template.
163	         * @param string   $name      The name of the specialized template.
164	         * @param string[] $templates Array of template files to search for, in order.
165	         */
166	        do_action( 'get_template_part', $slug, $name, $templates );
167	
168	        locate_template( $templates, true, false );
169	}
?>

 

WordPressのおすすめ-カスタマイズ編

おすすめ

 

スポンサードリンク

スポンサードリンク