• 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細

このWikiはいつでも誰でも編集できます

関数リファレンス/is page template

提供: WordPress Codex 日本語版
< 関数リファレンス
2015年6月1日 (月) 09:30時点におけるMiccweb (トーク | 投稿記録)による版 (新規追加)

(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

説明

This Conditional Tag allows you to determine if you are in any page template. Optionally checks if a specific Page Template is being used in a Page. This is a boolean function, meaning it returns either TRUE or FALSE. This tag must be used BEFORE The Loop and does not work inside The Loop (see Notes below).

使い方

<?php is_page_template( $template ); ?>

パラメータ

$template
string) (optional) Full template filename with ext
初期値: なし

返り値

(boolean) 
成功なら True、失敗なら false を返します。

用例

Is Page Template 'about' being used? Note that unlike with other conditionals, if you want to specify a particular Page Template, you need to use the filename, such as about.php or my_page_template.php.

if ( is_page_template( 'about.php' ) ) {
	// Returns true when 'about.php' is being used.
} else {
	// Returns false when 'about.php' is not being used.
}

Page template in subdirectory

If the page template is located in a subdirectory of the theme (since WP 3.4), prepend the folder name and a slash to the template filename, e.g.:

is_page_template( 'templates/about.php' );

Cannot Be Used Inside The Loop

Due to certain global variables being overwritten during The Loop is_page_template() will not work. In order to use it after The Loop you must call wp_reset_query() after The Loop.

Alternative

Since the page template slug is stored inside the post_meta for any post that has been assigned to a page template, it is possible to directly query the post_meta to see whether any given page has been assigned a page template. This is the method that is_page_template() uses internally.

The function get_page_template_slug( $post_id ) will return the slug of the currently assigned page template (or an empty string if no template has been assigned - or false if the $post_id does not correspond to an actual page). You can easily use this anywhere (in The Loop, or outside) to determine whether any page has been assigned a page template.

<?php 
  // in the loop:
  if ( get_page_template_slug( get_the_ID() ) ){
     // Yep, this page has a page template
  }

  // anywhere:
  if ( get_page_template_slug( $some_post_ID ) ){
     // Uh-huh.
  }
?>

更新履歴

新規導入: 2.5.0

ソースファイル

is_page_template()wp-includes/post-template.phpにあります。

関連

条件分岐タグis_404(), is_admin(), is_admin_bar_showing(), is_archive(), is_attachment(), is_author(), is_category(), is_comments_popup(), is_date(), is_day(), is_feed(), is_front_page(), is_home(), is_local_attachment(), is_main_query, /is_multi_author, is_month(), is_new_day(), is_page(), is_page_template(), is_paged(), is_plugin_active(), is_plugin_active_for_network() /en, is_plugin_inactive() /en, is_plugin_page() /en, is_post_type_archive(), is_preview() /en, is_search(), is_single(), is_singular(), is_sticky(), is_tag(), is_tax(), is_taxonomy_hierarchical(), is_time(), is_trackback(), is_year(), in_category(), in_the_loop(), is_active_sidebar(), is_active_widget(), is_blog_installed() /en, is_rtl(), is_dynamic_sidebar(), is_user_logged_in(), has_excerpt(), has_post_thumbnail(), has_tag(), pings_open(), email_exists(), post_type_exists(), taxonomy_exists(), term_exists(), username_exists() /en, wp_attachment_is_image(), wp_script_is()



関数リファレンステンプレートタグ目次もご覧ください。


このページ「関数リファレンス/is page template」は未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています

最新英語版: WordPress Codex » Function_Reference/is_page_template最新版との差分