- 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細)
「条件分岐タグ」の版間の差分
(→サイドバーの内容をページによって変更する: wp_list_cats 非推奨の注釈を追加(ryouji98さんさんありがとう!)) |
(2011-11-28T22:11:30 Jeffsebring 版に更新) |
||
6行目: | 6行目: | ||
これらのタグは[[テンプレート階層]]と深い関わりがあります。 | これらのタグは[[テンプレート階層]]と深い関わりがあります。 | ||
+ | |||
+ | '''注:''' 条件分岐タグが使えるのは WordPress の ''init'' アクションフックの後のみです。テーマの function.php 本文内(関数の外)では正しく動作しません。 | ||
<div id="The_Conditions_For_..."> | <div id="The_Conditions_For_..."> | ||
17行目: | 19行目: | ||
</div> | </div> | ||
− | ; <tt>is_home()</tt> : メインブログページが表示されている場合。 | + | ; <tt>[[関数リファレンス/is_home|is_home()]]</tt> : メインブログページが表示されている場合。 |
: This is the page which is showing the time based blog content of your site, so if you've set a static Page for the Front Page (see below), then this will only be true on the Page which you set as the "Posts page" in [[Administration_Panels|Administration]] > [[Administration_Panels#Reading|Settings]] > [[Settings_Reading_SubPanel|Reading]]. | : This is the page which is showing the time based blog content of your site, so if you've set a static Page for the Front Page (see below), then this will only be true on the Page which you set as the "Posts page" in [[Administration_Panels|Administration]] > [[Administration_Panels#Reading|Settings]] > [[Settings_Reading_SubPanel|Reading]]. | ||
24行目: | 26行目: | ||
</div> | </div> | ||
− | ; <tt>is_front_page()</tt> : サイトのフロントページが表示されている場合(投稿・[[Pages| | + | ; <tt>[[関数リファレンス/is_front_page|is_front_page()]]</tt> : サイトのフロントページが表示されている場合(投稿・[[Pages|固定ページ]]にかかわらず)。 |
: true を返すのは、[[Settings Reading SubPanel|表示設定]]の「トップページの表示」として | : true を返すのは、[[Settings Reading SubPanel|表示設定]]の「トップページの表示」として | ||
:* 「最新の投稿」を設定している場合には、メインブログページが表示されるとき | :* 「最新の投稿」を設定している場合には、メインブログページが表示されるとき | ||
− | :* 「固定ページ (以下を選択)」を設定している場合には、「フロントページ」として選択した[[Pages| | + | :* 「固定ページ (以下を選択)」を設定している場合には、「フロントページ」として選択した[[Pages|固定ページ]]が表示されるとき |
− | + | ||
<!-- | <!-- | ||
[[管理パネル]] > [[管理パネル#Settings|設定]] > [[Settings Reading SubPanel|表示設定]]画面: | [[管理パネル]] > [[管理パネル#Settings|設定]] > [[Settings Reading SubPanel|表示設定]]画面: | ||
42行目: | 44行目: | ||
</div> | </div> | ||
− | ; <tt>is_admin()</tt>: ダッシュボードまたは管理パネルが表示されている場合。 | + | ; <tt>[[関数リファレンス/is_admin|is_admin()]]</tt>: ダッシュボードまたは管理パネルが表示されている場合。 |
<div id="A_Single_Post_Page"> | <div id="A_Single_Post_Page"> | ||
48行目: | 50行目: | ||
</div> | </div> | ||
− | ; <tt>is_single()</tt> : | + | ; <tt>[[関数リファレンス/is_single|is_single()]]</tt> : 個別投稿のページ(または添付ファイルページ・カスタム投稿タイプの個別ページ)が表示されている場合。固定ページには適用されない。 |
− | ; <tt>is_single('17')</tt> : ID | + | ; <tt>is_single( '17' )</tt> : ID 17の投稿が表示されている場合。 |
− | ; <tt>is_single('Irish Stew')</tt> : "Irish Stew" | + | ; <tt>is_single( 'Irish Stew' )</tt> : "Irish Stew" というタイトルの投稿が表示されている場合。 |
− | ; <tt>is_single('beef-stew')</tt> : "beef-stew" | + | ; <tt>is_single( 'beef-stew' )</tt> : "beef-stew" という投稿スラッグの投稿が表示されている場合。 |
− | ; <tt>is_single(array(17,'beef-stew','Irish Stew'))</tt> : ID が 17、投稿スラッグが "beef-stew"、またはタイトルが "Irish Stew" | + | ; <tt>is_single( array( 17, 'beef-stew', 'Irish Stew' ) )</tt> : ID が 17、投稿スラッグが "beef-stew"、またはタイトルが "Irish Stew" のいずれかにあてはまる投稿が表示されている場合。 |
− | ; <tt>is_single(array(17, 19, 1, 11)) </tt> : Returns true when the single post being displayed is either ''post ID'' 17, ''post ID'' 19, ''post ID'' 1, or ''post ID'' 11. | + | ; <tt>is_single( array( 17, 19, 1, 11 ) )</tt> : Returns true when the single post being displayed is either ''post ID'' 17, ''post ID'' 19, ''post ID'' 1, or ''post ID'' 11. |
− | ; <tt>is_single(array('beef-stew', 'pea-soup', 'chili')) </tt> : Returns true when the single post being displayed is either the ''post_name'' "beef-stew", ''post_name'' "pea-soup" or ''post_name'' "chili". | + | ; <tt>is_single( array( 'beef-stew', 'pea-soup', 'chili' ) )</tt> : Returns true when the single post being displayed is either the ''post_name'' "beef-stew", ''post_name'' "pea-soup" or ''post_name'' "chili". |
− | ; <tt>is_single(array('Beef Stew', 'Pea Soup', 'Chili')) </tt> : Returns true when the single post being displayed is either the ''post_title'' "Beef Stew", ''post_title'' "Pea Soup" or ''post_title'' "Chili". | + | ; <tt>is_single( array( 'Beef Stew', 'Pea Soup', 'Chili' ) )</tt> : Returns true when the single post being displayed is either the ''post_title'' "Beef Stew", ''post_title'' "Pea Soup" or ''post_title'' "Chili". |
− | 注: | + | 注: この関数は投稿 ID、投稿タイトル、または投稿名を区別しません。投稿 ID が「17」の投稿をリクエストした場合、タイトルや投稿スラッグが「17」の投稿が表示されることがあります。 |
<div id="A_Sticky_Post"> | <div id="A_Sticky_Post"> | ||
=== 先頭固定表示の投稿 === | === 先頭固定表示の投稿 === | ||
</div> | </div> | ||
− | ; <tt>is_sticky()</tt> : 投稿編集ページで「この投稿を先頭に固定表示」のチェックボックスがついている投稿が表示されている場合。もし ID 変数が指定されていない場合、投稿 ID は [[The Loop|WordPress ループ]]の投稿のものになる。 | + | ; <tt>[[関数リファレンス/is_sticky|is_sticky()]]</tt> : 投稿編集ページで「この投稿を先頭に固定表示」のチェックボックスがついている投稿が表示されている場合。もし ID 変数が指定されていない場合、投稿 ID は [[The Loop|WordPress ループ]]の投稿のものになる。 |
− | + | ; <tt>is_sticky( '17' )</tt> : ID 17 の投稿が先頭固定表示の場合、真を返す。 | |
− | ; <tt>is_sticky('17')</tt> : ID 17 の投稿が先頭固定表示の場合、真を返す。 | + | |
<div id="A_Post_Type"> | <div id="A_Post_Type"> | ||
=== 投稿タイプ === | === 投稿タイプ === | ||
</div> | </div> | ||
− | ; <tt>get_post_type()</tt> : Not really a conditional tag, but returns the [[Function_Reference/register_post_type|registered post type]] of the current post. | + | ; <tt>[[関数リファレンス/get_post_type|get_post_type()]]</tt> : Not really a conditional tag, but returns the [[Function_Reference/register_post_type|registered post type]] of the current post. |
− | ; <tt> if( | + | ; <tt> if( 'book' == get_post_type() ) ...</tt> : Tests to see if the current post is of type 'book'. |
− | ; <tt>post_type_exists()</tt> : Returns true if a given post type is a [[Function_Reference/register_post_type|registered post type]]. This does not test if a post is a certain post_type. Note: This function replaces a function called ''is_post_type'' which existed briefly in 3.0 development. | + | ; <tt>[[関数リファレンス/post_type_exists|post_type_exists()]]</tt> : Returns true if a given post type is a [[Function_Reference/register_post_type|registered post type]]. This does not test if a post is a certain post_type. Note: This function replaces a function called ''is_post_type'' which existed briefly in 3.0 development. |
<div id="A_Post_Type_is_Hierarchical"> | <div id="A_Post_Type_is_Hierarchical"> | ||
=== 階層を持つ投稿タイプ === | === 階層を持つ投稿タイプ === | ||
</div> | </div> | ||
− | ; <tt>is_post_type_hierarchical($post_type)</tt> : Returns true if this $post_type has been set with [[Function_Reference/register_post_type|hierarchical support when registered | + | ; <tt>[[関数リファレンス/is_post_type_hierarchical|is_post_type_hierarchical($post_type)]]</tt> : Returns true if this $post_type has been set with [[Function_Reference/register_post_type|hierarchical support when registered]]. |
− | ; <tt>is_post_type_hierarchical('book')</tt> : Returns true if the book post type was registered as having support for hierarchical. | + | ; <tt>is_post_type_hierarchical( 'book' )</tt> : Returns true if the book post type was registered as having support for hierarchical. |
+ | |||
+ | <div id="A_Post_Type_Archive"> | ||
+ | === 投稿タイプアーカイブ === | ||
+ | </div> | ||
+ | |||
+ | :<tt>[[関数リファレンス/is_post_type_archive|is_post_type_archive()]]</tt> : Returns true on any post type archive. | ||
+ | :<tt>is_post_type_archive( $post_type )</tt> : Returns true if on a post type archive page that matches $post_type (can be a single post type or an array of post types). | ||
+ | |||
+ | To turn on post type archives, use 'has_archive' => true, when [http://codex.wordpress.org/Function_Reference/register_post_type registering the post type]. | ||
<div id="A_Comments_Popup"> | <div id="A_Comments_Popup"> | ||
=== コメントポップアップ === | === コメントポップアップ === | ||
</div> | </div> | ||
− | ; <tt>is_comments_popup()</tt> : コメントポップアップウィンドウが表示されている場合。 | + | ; <tt>[[関数リファレンス/is_comments_popup|is_comments_popup()]]</tt> : コメントポップアップウィンドウが表示されている場合。 |
<div id="Any_Page_Containing_Posts"> | <div id="Any_Page_Containing_Posts"> | ||
− | === | + | === 投稿を含むページ === |
</div> | </div> | ||
− | ; <tt>comments_open()</tt>: [[The Loop|WordPress ループ]] | + | ; <tt>[[関数リファレンス/comments_open|comments_open()]]</tt>: [[The Loop|WordPress ループ]]内で処理中の投稿がコメント受信を受け付けている場合。 |
− | ; <tt>pings_open()</tt>: [[The Loop|WordPress ループ]] | + | ; <tt>[[関数リファレンス/pings_open|pings_open()]]</tt>: [[The Loop|WordPress ループ]]内で処理中の投稿がピン(ピンバックおよびトラックバック)を受け付けている場合。 |
<div id="A_PAGE_Page"> | <div id="A_PAGE_Page"> | ||
95行目: | 105行目: | ||
</div> | </div> | ||
− | ここでいう「ページ」とは、WordPressにおける[[Pages|固定ページ]]のことで、いわゆる「Web | + | ここでいう「ページ」とは、WordPressにおける[[Pages|固定ページ]]のことで、いわゆる「Web ページ」とは異なります。つまり、''post_type'' が 'page' となっているもののことです。詳しくは、「[[Pages#用語について|用語について]]」をご覧ください。 |
− | ; <tt>is_page()</tt> : [[Pages| | + | ; <tt>[[関数リファレンス/is_page|is_page()]]</tt> : [[Pages|固定ページ]]が表示されている場合。 |
− | ; <tt>is_page('42')</tt> : ID 42の[[Pages| | + | ; <tt>is_page( '42' )</tt> : ID 42の[[Pages|固定ページ]]が表示されている場合。 |
− | ; <tt>is_page('About Me And Joe')</tt> : "About Me And Joe"というタイトルの[[Pages| | + | ; <tt>is_page( 'About Me And Joe' )</tt> : "About Me And Joe"というタイトルの[[Pages|固定ページ]]が表示されている場合。 |
− | ; <tt>is_page('about-me')</tt> : "about-me"という投稿スラッグの[[Pages| | + | ; <tt>is_page( 'about-me' )</tt> : "about-me"という投稿スラッグの[[Pages|固定ページ]]が表示されている場合。 |
− | ; <tt>is_page(array(42,'about-me','About Me And Joe'))</tt> : ID が 42、投稿スラッグが "about-me"、またはタイトルが "About Me And Joe" のいずれかにあてはまる[[Pages| | + | ; <tt>is_page( array( 42, 'about-me', 'About Me And Joe' ) )</tt> : ID が 42、投稿スラッグが "about-me"、またはタイトルが "About Me And Joe" のいずれかにあてはまる[[Pages|固定ページ]]が表示されている場合。 |
− | : | + | ; <tt>is_page( array( 42, 54, 6 ) )</tt> : ID が 42、54、または6のいずれかの[[Pages|固定ページ]]が表示されている場合。 |
+ | |||
+ | <div id="Testing_for_paginated_Pages"> | ||
+ | ==== 複数にわたるページの一部かどうかをチェックする ==== | ||
+ | </div> | ||
+ | You can use this code to check whether you're on the nth page in a Post or PAGE Page that has been divided into pages using the <tt><!<nowiki></nowiki>--nextpage--></tt> QuickTag. This can be useful, for example, if you wish to display meta-data only on the first page of a post divided into several pages. | ||
+ | |||
+ | '''例 1''' | ||
+ | <pre><?php | ||
+ | $paged = $wp_query->get( 'paged' ); | ||
+ | |||
+ | if ( ! $paged || $paged < 2 ) | ||
+ | { | ||
+ | // 複数にわたるページの一部ではない場合 (または複数にわたる投稿・固定ページの最初のページの場合) | ||
+ | |||
+ | } | ||
+ | else | ||
+ | { | ||
+ | // 複数にわたるページの一部である場合 | ||
+ | |||
+ | } | ||
+ | ?> | ||
+ | </pre> | ||
+ | |||
+ | '''例 2''' | ||
+ | <!-- Could someone please take a look at the difference between 'paged' & 'page'? I ran into some support requests, where people said it's 'page', not 'paged' --> | ||
+ | |||
+ | <pre><?php | ||
+ | $paged = get_query_var( 'page' ) ? get_query_var( 'page' ) : false; | ||
+ | if ( $paged === false ) | ||
+ | { | ||
+ | // 複数ページの一部ではない場合 (または投稿・固定ページの複数ページの最初のページの場合) | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | // 複数ページの一部である場合 | ||
+ | } | ||
+ | ?></pre> | ||
<div id="Testing_for_sub-Pages"> | <div id="Testing_for_sub-Pages"> | ||
128行目: | 175行目: | ||
global $post; // $post には現在の固定ページの情報があります | global $post; // $post には現在の固定ページの情報があります | ||
if ( is_page() && $post->post_parent ) { // 現在の固定ページが親ページを持つかどうかをチェックします | if ( is_page() && $post->post_parent ) { // 現在の固定ページが親ページを持つかどうかをチェックします | ||
− | + | return $post->post_parent; // 親ページの ID を返します | |
− | + | ||
} else { // 親ページを持たないので... | } else { // 親ページを持たないので... | ||
return false; // ...false を返します | return false; // ...false を返します | ||
143行目: | 189行目: | ||
<pre> | <pre> | ||
<?php | <?php | ||
− | + | if ( is_page( 'about' ) || '2' == $post->post_parent ) { | |
− | if ( is_page('about') || $post->post_parent | + | |
// このページは "紹介"、または親ページが "紹介" | // このページは "紹介"、または親ページが "紹介" | ||
$bannerimg = 'about.jpg'; | $bannerimg = 'about.jpg'; | ||
− | } elseif ( is_page('learning') || $post->post_parent | + | } elseif ( is_page( 'learning' ) || '56' == $post->post_parent ) { |
$bannerimg = 'teaching.jpg'; | $bannerimg = 'teaching.jpg'; | ||
− | } elseif ( is_page('admissions') || $post->post_parent | + | } elseif ( is_page( 'admissions' ) || '15' == $post->post_parent ) { |
$bannerimg = 'admissions.jpg'; | $bannerimg = 'admissions.jpg'; | ||
165行目: | 210行目: | ||
'''スニペット 4''' | '''スニペット 4''' | ||
<pre> | <pre> | ||
− | function is_tree($pid) { // $pid = 指定したページの ID | + | function is_tree( $pid ) { // $pid = 指定したページの ID |
global $post; // $post に現在の固定ページの情報をロード | global $post; // $post に現在の固定ページの情報をロード | ||
− | + | ||
− | + | if ( is_page($pid) ) | |
− | + | return true; // その固定ページまたはサブページの場合 | |
− | + | ||
− | + | $anc = get_post_ancestors( $post->ID ); | |
− | + | foreach ( $anc as $ancestor ) { | |
− | + | if( is_page() && $ancestor == $pid ) { | |
− | + | return true; | |
− | + | } | |
− | + | } | |
− | } | + | |
+ | return false; // その固定ページではない、または親ページではない場合 | ||
+ | } | ||
</pre> | </pre> | ||
− | スニペット 4 を [[テーマの作成#Theme Functions File|functions.php]] ファイルに追加して、<tt>is_tree('id')</tt> を実行すると現在のページが指定したページ、またはそのサブページであるかどうかがわかります。例えば、スニペット 3 の始めの if ブロック内にある "<tt>is_page('about') || $post->post_parent | + | スニペット 4 を [[テーマの作成#Theme Functions File|functions.php]] ファイルに追加して、<tt>is_tree( 'id' )</tt> を実行すると現在のページが指定したページ、またはそのサブページであるかどうかがわかります。例えば、スニペット 3 の始めの if ブロック内にある "<tt>is_page( 'about' ) || '2' == $post->post_parent</tt>" は <tt>is_tree( '2' )</tt> に置き換えられます。 |
注意:2階層以上のページ階層がある場合は、親ページは現在より1階層上のページを差します。ページ階層の最上にあるページではないことに注意してください。 | 注意:2階層以上のページ階層がある場合は、親ページは現在より1階層上のページを差します。ページ階層の最上にあるページではないことに注意してください。 | ||
187行目: | 234行目: | ||
==== ページテンプレート ==== | ==== ページテンプレート ==== | ||
</div> | </div> | ||
− | + | この条件分岐タグを使うと、特定の[[Pages#ページテンプレート|ページテンプレート]]を使っているかどうかをチェックできます。 | |
− | ; <tt>is_page_template()</tt> : [[Pages#ページテンプレート|ページテンプレート]]が使われている場合。 | + | ; <tt>[[関数リファレンス/is_page_template|is_page_template()]]</tt> : [[Pages#ページテンプレート|ページテンプレート]]が使われている場合。 |
; <tt>is_page_template('about.php')</tt> : "about" という[[Pages#ページテンプレート|ページテンプレート]]が使われている場合。他の条件分岐タグとは違い、特定のテンプレートを選びたい場合には about.php などのファイル名を指定する必要があります。 | ; <tt>is_page_template('about.php')</tt> : "about" という[[Pages#ページテンプレート|ページテンプレート]]が使われている場合。他の条件分岐タグとは違い、特定のテンプレートを選びたい場合には about.php などのファイル名を指定する必要があります。 | ||
196行目: | 243行目: | ||
</div> | </div> | ||
− | ; <tt>is_category()</tt> : あるカテゴリーのアーカイブページが表示されている場合。 | + | ; <tt>[[関数リファレンス/is_category|is_category()]]</tt> : あるカテゴリーのアーカイブページが表示されている場合。 |
− | ; <tt>is_category('9')</tt> : カテゴリーID 9のアーカイブページが表示されている場合。 | + | ; <tt>is_category( '9' )</tt> : カテゴリーID 9のアーカイブページが表示されている場合。 |
− | ; <tt>is_category('Stinky Cheeses')</tt> : "Stinky Cheeses"というカテゴリーのアーカイブページが表示されている場合。 | + | ; <tt>is_category( 'Stinky Cheeses' )</tt> : "Stinky Cheeses"というカテゴリーのアーカイブページが表示されている場合。 |
− | ; <tt>is_category('blue-cheese')</tt> : "blue-cheese"というカテゴリースラッグのアーカイブページが表示されている場合。 | + | ; <tt>is_category( 'blue-cheese' )</tt> : "blue-cheese"というカテゴリースラッグのアーカイブページが表示されている場合。 |
− | ; <tt>is_category(array(9,'blue-cheese','Stinky Cheeses'))</tt> : 投稿のカテゴリーが term_ID 9、スラッグが "blue-cheese" の場合、カテゴリー名が "Stinky Cheeses" のいずれかにあてはまる場合、真を返す。 | + | ; <tt>is_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) )</tt> : 投稿のカテゴリーが term_ID 9、スラッグが "blue-cheese" の場合、カテゴリー名が "Stinky Cheeses" のいずれかにあてはまる場合、真を返す。 |
− | + | ; <tt>'''in'''_category( '5' )</tt> : (注: '''in'''_category です。) 現在の投稿がカテゴリーID 5に'''属する'''場合にtrueを返します。 [[テンプレートタグ/in_category|詳細]] | |
− | ; <tt>'''in'''_category('5')</tt> : (注: '''in'''_category です。) | + | |
; <tt>in_category( array( 1,2,3 ) )</tt> : Returns true if the current post is '''in''' either category 1, 2, or 3. | ; <tt>in_category( array( 1,2,3 ) )</tt> : Returns true if the current post is '''in''' either category 1, 2, or 3. | ||
− | ; <tt>!in_category( array( 4,5,6 ) )</tt> : Returns true if the current post is '''NOT in''' either category 4, 5, or 6. Note the '''!''' at the beginning. | + | ; <tt>! in_category( array( 4,5,6 ) )</tt> : Returns true if the current post is '''NOT in''' either category 4, 5, or 6. Note the '''!''' at the beginning. |
<tt>[[#Any Archive Page|is_archive()]]</tt> と[[Category Templates|カテゴリーテンプレート]]も参照してください。 | <tt>[[#Any Archive Page|is_archive()]]</tt> と[[Category Templates|カテゴリーテンプレート]]も参照してください。 | ||
212行目: | 258行目: | ||
</div> | </div> | ||
− | ; <tt>is_tag()</tt> : タグのアーカイブページが表示されている場合。 | + | ; <tt>[[関数リファレンス/is_tag|is_tag()]]</tt> : タグのアーカイブページが表示されている場合。 |
− | ; <tt>is_tag('mild')</tt> : 'mild' というスラッグのついたタグのアーカイブページが表示されている場合。 | + | ; <tt>is_tag( 'mild' )</tt> : 'mild' というスラッグのついたタグのアーカイブページが表示されている場合。 |
− | ; <tt>is_tag(array('sharp','mild','extreme'))</tt> : 'sharp' または 'mild' または 'extreme' というスラッグのついたタグのアーカイブページが表示されている場合。 | + | ; <tt>is_tag( array( 'sharp', 'mild', 'extreme' ) )</tt> : 'sharp' または 'mild' または 'extreme' というスラッグのついたタグのアーカイブページが表示されている場合。 |
− | + | ; <tt>has_tag()</tt> : タグのある投稿がループ内で表示されている場合。 | |
− | ; <tt>has_tag()</tt> : タグのある投稿がループ内で表示されている場合。 | + | ; <tt>has_tag( 'mild' )</tt> : 'mild' というタグのある投稿が表示されている場合。 |
− | ; <tt>has_tag('mild')</tt> : 'mild' というタグのある投稿が表示されている場合。 | + | ; <tt>has_tag( array( 'sharp', 'mild', 'extreme' ) )</tt> : 配列内のいずれかのタグのある投稿が表示されている場合。 |
− | ; <tt>has_tag(array('sharp','mild','extreme'))</tt> : 配列内のいずれかのタグのある投稿が表示されている場合。 | + | |
− | <tt>[[#Any Archive Page|is_archive()]]</tt> と [[Tag Templates|タグテンプレート]]/[[:en:Tag Templates|en]] | + | <tt>[[#Any Archive Page|is_archive()]]</tt> と [[Tag Templates|タグテンプレート]]/[[:en:Tag Templates|en]] もあわせてお読みください。 |
<div id="A_Taxonomy_Page"> | <div id="A_Taxonomy_Page"> | ||
226行目: | 271行目: | ||
</div> | </div> | ||
− | ; <tt>is_tax()</tt> : タクソノミーのアーカイブページが表示されている場合。 | + | ; <tt>[[関数リファレンス/is_tax|is_tax()]]</tt> : タクソノミーのアーカイブページが表示されている場合。 |
− | ; <tt>is_tax(' | + | ; <tt>is_tax( 'flavor' )</tt> : 'flavor' というスラッグのついたタクソノミーのアーカイブページが表示されている場合。 |
− | ; <tt>is_tax(array('sharp','mild','extreme'))</tt> : 'sharp' または 'mild' または 'extreme' | + | ; <tt>is_tax( 'flavor', array( 'sharp', 'mild', 'extreme' ) )</tt> : 'sharp' または 'mild' または 'extreme' というスラッグのついた flavor タクソノミーのアーカイブページが表示されている場合。 |
− | <tt>[[#Any Archive Page|is_archive()]]</tt> | + | <tt>[[#Any Archive Page|is_archive()]]</tt> もあわせてお読みください。 |
<div id="A_Registered_Taxonomy"> | <div id="A_Registered_Taxonomy"> | ||
− | === | + | === 登録タクソノミー === |
</div> | </div> | ||
− | ; <tt> | + | ; <tt>[[関数リファレンス/taxonomy_exists|taxonomy_exists()]]</tt> : あるタクソノミーが [[関数リファレンス/register_taxonomy|register_taxonomy()]] を使って登録されている場合。バージョン 3.0 で非推奨になるまでは is_taxonomy() と呼ばれていた。 |
<div id="An_Author_Page"> | <div id="An_Author_Page"> | ||
242行目: | 287行目: | ||
</div> | </div> | ||
− | ; <tt>is_author()</tt> : 作成者のアーカイブページが表示されている場合。 | + | ; <tt>[[関数リファレンス/is_author|is_author()]]</tt> : 作成者のアーカイブページが表示されている場合。 |
− | ; <tt>is_author('4')</tt> : ID 4 の作成者のアーカイブページを表示している場合。 | + | ; <tt>is_author( '4' )</tt> : ID 4 の作成者のアーカイブページを表示している場合。 |
− | ; <tt>is_author('Vivian')</tt> : ニックネームが"Vivian"のアーカイブページを表示している場合。 | + | ; <tt>is_author( 'Vivian' )</tt> : ニックネームが"Vivian"のアーカイブページを表示している場合。 |
− | ; <tt>is_author('john-jones')</tt> : "john-jones" という「ナイスネーム」を持つ作成者のアーカイブページを表示している場合。 | + | ; <tt>is_author( 'john-jones' )</tt> : "john-jones" という「ナイスネーム」を持つ作成者のアーカイブページを表示している場合。 |
− | ; <tt>is_author(array(4,'john-jones','Vivian'))</tt> : ID が4、ナイスネームが "john-jones"、またはニックネームが "Vivian" という作成者のアーカイブページを表示している場合。 | + | ; <tt>is_author( array( 4, 'john-jones' , 'Vivian' ) )</tt> : ID が4、ナイスネームが "john-jones"、またはニックネームが "Vivian" という作成者のアーカイブページを表示している場合。 |
− | + | ||
− | <tt>[[#Any Archive Page|is_archive()]]</tt> や[[Author Templates|作成者テンプレート]]/[[:en:Author Templates|en]] | + | <tt>[[#Any Archive Page|is_archive()]]</tt> や[[Author Templates|作成者テンプレート]]/[[:en:Author Templates|en]] もあわせてお読みください。 |
+ | |||
+ | <div id="A_Multi-author_Site"> | ||
+ | === 作成者ページ === | ||
+ | </div> | ||
+ | |||
+ | ; <tt>[[関数リファレンス/is_multi_author|is_multi_author()]]</tt> : サイト上に一人以上投稿を公開しているユーザーがいる場合。[[Version 3.2|バージョン 3.2]] から導入された。 | ||
+ | |||
+ | <tt>[[#Any Archive Page|is_archive()]]</tt> や[[Author Templates|作成者テンプレート]]/[[:en:Author Templates|en]] もあわせてお読みください。 | ||
<div id="A_Date_Page"> | <div id="A_Date_Page"> | ||
255行目: | 307行目: | ||
</div> | </div> | ||
− | ; <tt>is_date()</tt> : 日付別のアーカイブページのいずれかが表示されている場合。(例:月別、年別、日別、時間別) | + | ; <tt>[[関数リファレンス/is_date|is_date()]]</tt> : 日付別のアーカイブページのいずれかが表示されている場合。(例:月別、年別、日別、時間別) |
− | ; <tt>is_year()</tt> : 年別のアーカイブページが表示されている場合。 | + | ; <tt>[[関数リファレンス/is_year|is_year()]]</tt> : 年別のアーカイブページが表示されている場合。 |
− | ; <tt>is_month()</tt> : 月別のアーカイブページが表示されている場合。 | + | ; <tt>[[関数リファレンス/is_month|is_month()]]</tt> : 月別のアーカイブページが表示されている場合。 |
− | ; <tt>is_day()</tt> : 日別のアーカイブページが表示されている場合。 | + | ; <tt>[[関数リファレンス/is_day|is_day()]]</tt> : 日別のアーカイブページが表示されている場合。 |
− | ; <tt>is_time()</tt> : 毎時別、毎分別、毎秒別のアーカイブページが表示されている場合。 | + | ; <tt>[[関数リファレンス/is_time|is_time()]]</tt> : 毎時別、毎分別、毎秒別のアーカイブページが表示されている場合。 |
+ | ; <tt>[[関数リファレンス/is_new_day|is_new_day()]]</tt> : 投稿の日付が新しい日の場合。ループ内で使う。 | ||
− | <tt>[[#Any Archive Page|is_archive()]]</tt> | + | <tt>[[#Any Archive Page|is_archive()]]</tt> もあわせてお読みください。 |
<div id="Any_Archive_Page"> | <div id="Any_Archive_Page"> | ||
267行目: | 320行目: | ||
</div> | </div> | ||
− | ; <tt>is_archive()</tt> : 各アーカイブページが表示されている場合。アーカイブページには、カテゴリー、タグ、作成者、日付別のものがあります。 | + | ; <tt>[[関数リファレンス/is_archive|is_archive()]]</tt> : 各アーカイブページが表示されている場合。アーカイブページには、カテゴリー、タグ、作成者、日付別のものがあります。 |
<div id="A_Search_Result_Page"> | <div id="A_Search_Result_Page"> | ||
273行目: | 326行目: | ||
</div> | </div> | ||
− | ; <tt>is_search()</tt> : 検索結果のページが表示されている場合。 | + | ; <tt>[[関数リファレンス/is_search|is_search()]]</tt> : 検索結果のページが表示されている場合。 |
<div id="A_404_Not_Found_Page"> | <div id="A_404_Not_Found_Page"> | ||
279行目: | 332行目: | ||
</div> | </div> | ||
− | ; <tt>is_404()</tt> : "HTTP 404: Not Found"エラーページが表示されている場合。 | + | ; <tt>[[関数リファレンス/is_404|is_404()]]</tt> : "HTTP 404: Not Found"エラーページが表示されている場合。 |
<div id="A_Paged_Page"> | <div id="A_Paged_Page"> | ||
285行目: | 338行目: | ||
</div> | </div> | ||
− | ; <tt>is_paged()</tt> : | + | ; <tt>[[関数リファレンス/is_paged|is_paged()]]</tt> : 表示中のページが複数のページにわたる場合。これは例えばアーカイブページやメインページに表示する投稿数よりも多い投稿があり、複数ページに分かれているときを指し、2ページ目以降のときに true を返します。ただし、1つの投稿や[[Pages|固定ページ]]の本文が <tt><nowiki><!--nextpage--></nowiki></tt> [[Writing Posts#Visual Versus HTML Editor|クイックタグ]]で複数ページに分けられている場合は当てはまりません。投稿または固定ページが <tt><!<nowiki></nowiki>--nextpage--></tt> クイックタグを使って分割されているかどうか調べるには、[[#Testing_for_paginated_Pages|複数にわたるページの一部かどうかをチェックする]]をご覧ください。 |
<div id="An_Attachment"> | <div id="An_Attachment"> | ||
291行目: | 344行目: | ||
</div> | </div> | ||
− | ; <tt>is_attachment()</tt> : | + | ; <tt>[[関数リファレンス/is_attachment|is_attachment()]]</tt> : 投稿または[[Pages|固定ページ]]の添付ファイルが表示されている場合。添付ファイルは、投稿画面のメディアアップロード機能を使ってアップロードした画像などのファイルの事で、テンプレートを使って独自のページ内に表示できます。詳細は、 [[Using Image and File Attachments|画像や添付ファイルの使い方]]/[[:en:Using Image and File Attachments|en]] をご覧ください。 |
<div id="A_Single_Page.2C_Single_Post_or_Attachment"> | <div id="A_Single_Page.2C_Single_Post_or_Attachment"> | ||
=== シングルページ (固定ページ、個別投稿ページ、添付ファイルページ) === | === シングルページ (固定ページ、個別投稿ページ、添付ファイルページ) === | ||
</div> | </div> | ||
− | ; <tt>is_singular()</tt> : <tt>is_single()</tt>、<tt>is_page()</tt> 、<tt>is_attachment()</tt> のいずれかが真である場合。 | + | ; <tt>[[関数リファレンス/is_singular|is_singular()]]</tt> : <tt>is_single()</tt>、<tt>is_page()</tt> 、<tt>is_attachment()</tt> のいずれかが真である場合。 |
− | ; <tt>is_singular('book')</tt> : | + | ; <tt>is_singular( 'book' )</tt> : 'book' という[[投稿タイプ|カスタム投稿タイプ]]の投稿を表示している場合。 |
− | ; <tt>is_singular(array( 'newspaper', 'book' ))</tt> : | + | ; <tt>is_singular( array( 'newspaper', 'book' ) )</tt> : 'newspaper' または 'book' という[[投稿タイプ|カスタム投稿タイプ]]の投稿を表示している場合。 |
<div id="A_Syndication"> | <div id="A_Syndication"> | ||
304行目: | 357行目: | ||
</div> | </div> | ||
− | ; <tt>is_feed()</tt> : [[ブログ入門#Syndication|Syndication]](フィード)がリクエストされた場合。 | + | ; <tt>[[関数リファレンス/is_feed|is_feed()]]</tt> : [[ブログ入門#Syndication|Syndication]](フィード)がリクエストされた場合。 |
:このタグは通常、一般のユーザーがページを表示しているときに条件を判断するためではなく、プラグイン開発の際に WordPress 内部で使われます。 | :このタグは通常、一般のユーザーがページを表示しているときに条件を判断するためではなく、プラグイン開発の際に WordPress 内部で使われます。 | ||
311行目: | 364行目: | ||
</div> | </div> | ||
− | ; <tt>is_trackback()</tt> : フックの中にトラックバックの機構が組み込まれている場合。 | + | ; <tt>[[関数リファレンス/is_trackback|is_trackback()]]</tt> : フックの中にトラックバックの機構が組み込まれている場合。 |
:このタグは通常、一般のユーザーがページを表示しているときに条件を判断するためではなく、プラグイン開発の際に WordPress 内部で使われます。 | :このタグは通常、一般のユーザーがページを表示しているときに条件を判断するためではなく、プラグイン開発の際に WordPress 内部で使われます。 | ||
318行目: | 371行目: | ||
</div> | </div> | ||
− | ; <tt>is_preview()</tt> : 未公開モードで固定リンクページを表示している場合。 | + | ; <tt>[[関数リファレンス/is_preview|is_preview()]]</tt> : 未公開モードで固定リンクページを表示している場合。 |
<div id="Has_An_Excerpt"> | <div id="Has_An_Excerpt"> | ||
=== 抜粋あり === | === 抜粋あり === | ||
</div> | </div> | ||
− | ; <tt>has_excerpt()</tt> : 投稿に (手動で書かれた) 抜粋がある場合。 | + | ; <tt>[[関数リファレンス/has_excerpt|has_excerpt()]]</tt> : 投稿に (手動で書かれた) 抜粋がある場合。 |
− | ; <tt>has_excerpt('42')</tt> : 投稿 ID 42 の投稿に抜粋がある場合。 | + | ; <tt>has_excerpt( '42' )</tt> : 投稿 ID 42 の投稿に抜粋がある場合。 |
<pre><?php | <pre><?php | ||
330行目: | 383行目: | ||
global $post; | global $post; | ||
− | if ( empty($post->post_excerpt) ) { | + | if ( empty( $post->post_excerpt ) ) { |
// 投稿に抜粋がない場合 | // 投稿に抜粋がない場合 | ||
} else { | } else { | ||
342行目: | 395行目: | ||
<pre> | <pre> | ||
− | <?php if ( !has_excerpt()) { | + | <?php if ( ! has_excerpt() ) { |
echo ''; | echo ''; | ||
} else { | } else { | ||
351行目: | 404行目: | ||
Replace auto excerpt for a text or code. | Replace auto excerpt for a text or code. | ||
<pre> | <pre> | ||
− | <?php if ( !has_excerpt() ) {?> | + | <?php if ( ! has_excerpt() ) {?> |
<!-- you text or code --> | <!-- you text or code --> | ||
<?php } ?> | <?php } ?> | ||
</pre> | </pre> | ||
+ | |||
+ | <div id="Has_A_Nav_Menu_Assigned"> | ||
+ | === ナビゲーションメニュー割り当てあり === | ||
+ | </div> | ||
+ | |||
+ | ; <tt>[[関数リファレンス/has_nav_menu|has_nav_menu()]]</tt> : 登録済みのナビゲーションメニュー一が割り当てられているかどうかをチェックする。割り当て済み (true) または割り当てなし (false) を返す。 | ||
<div id="Inside_The_Loop"> | <div id="Inside_The_Loop"> | ||
=== ループ内 === | === ループ内 === | ||
</div> | </div> | ||
− | ; <tt>in_the_loop()</tt> : | + | ; <tt>[[関数リファレンス/in_the_loop|in_the_loop()]]</tt> : ループ内かどうかをチェックする。ループ内にあった場合真を返すので、プラグイン作者に便利。 |
<div id="Is_Sidebar_Active"> | <div id="Is_Sidebar_Active"> | ||
=== サイドバー使用中 === | === サイドバー使用中 === | ||
</div> | </div> | ||
− | ; <tt>is_active_sidebar()</tt> : 指定したダイナミックサイドバーが使用中の場合、真を返す。サイドバー名、ID、番号を指定できる。 | + | ; <tt>[[関数リファレンス/is_active_sidebar|is_active_sidebar()]]</tt> : 指定したダイナミックサイドバーが使用中の場合、真を返す。サイドバー名、ID、番号を指定できる。 |
− | : | + | |
+ | <div id="Part_of_a_Network _Multisite"> | ||
+ | === ネットワークの一部(マルチサイト) === | ||
+ | </div> | ||
+ | ; <tt>[[関数リファレンス/is_multisite is_multisite|is_multisite is_multisite()]]</tt> : 現在のサイトがマルチサイトインストールの一部かどうかチェックする。 | ||
+ | |||
+ | <div id="Admin_of_a_Network _Multisite"> | ||
+ | === ネットワークの管理者(マルチサイト) === | ||
+ | </div> | ||
+ | ; <tt>[[関数リファレンス/is_super_admin|is_super_admin()]]</tt> : ユーザーがネットワークの特権管理者かどうかをチェックする。 | ||
+ | |||
+ | <div id="An_Active_Plugin"> | ||
+ | === 有効化済みプラグイン === | ||
+ | </div> | ||
+ | ; <tt>[[関数リファレンス/is_plugin_active|is_plugin_active()]]</tt> : プラグインが有効化されているかチェックする。 | ||
+ | |||
+ | <div id="Theme_supports_a_feature"> | ||
+ | === 機能対応のテーマ === | ||
+ | </div> | ||
+ | ; <tt>[[関数リファレンス/current_theme_supports|current_theme_supports()]]</tt> : テーマに機能が存在するかチェックする。 | ||
<div id="Working_Examples"> | <div id="Working_Examples"> | ||
374行目: | 452行目: | ||
<div id="Single_Post"> | <div id="Single_Post"> | ||
− | === | + | === 個別投稿 === |
</div> | </div> | ||
− | 以下は<tt>is_single()</tt> | + | 以下は<tt>is_single()</tt>によって特定の情報を個別投稿のページでのみ表示させるための例文です。 |
<pre> | <pre> | ||
if ( is_single() ) { | if ( is_single() ) { | ||
− | echo ' | + | echo 'この投稿は' . single_cat_title() . 'カテゴリーに属しています'; |
} | } | ||
</pre> | </pre> | ||
388行目: | 466行目: | ||
<pre> | <pre> | ||
− | if (is_home() || is_single()) { | + | if ( is_home() || is_single() ) { |
the_content(); | the_content(); | ||
} | } | ||
413行目: | 491行目: | ||
<?php | <?php | ||
// 日付別アーカイブページかどうかをチェック | // 日付別アーカイブページかどうかをチェック | ||
− | if (is_date()) | + | if ( is_date() ) { |
− | { | + | if ( date( 'Y' ) != get_the_date( 'Y' ) ) { |
− | if (date('Y') != get_the_date('Y')) | + | // 今年の投稿ではない場合、"oldentry" クラスを追加してスタイルを変える |
− | + | ||
− | // | + | |
echo '<div class="oldentry">'; | echo '<div class="oldentry">'; | ||
} else { | } else { | ||
425行目: | 501行目: | ||
echo '<div class="entry">'; | echo '<div class="entry">'; | ||
} | } | ||
− | the_content('続きを読む »'); | + | the_content( '続きを読む »' ); |
?> | ?> | ||
</div> | </div> | ||
443行目: | 519行目: | ||
<?php | <?php | ||
// 表示中のページに合わせたサイドバーコンテンツを生成 | // 表示中のページに合わせたサイドバーコンテンツを生成 | ||
− | if (is_home()) { | + | if ( is_home() ) { |
− | + | // ホームページを表示している際、トップレベルカテゴリーの一覧を表示 | |
− | + | echo "<ul>"; | |
− | + | wp_list_cats('optionall=0&sort_column=name&list=1&children=0'); | |
− | + | echo "</ul>"; | |
− | } elseif (is_category()) { | + | } elseif ( is_category() ) { |
− | + | // カテゴリーアーカイブページを表示している際、そのカテゴリー内のサブカテゴリーを含む一覧を表示 | |
− | + | echo "<ul>"; | |
− | + | wp_list_cats('optionall=1&sort_column=name&list=1&children=1&hierarchical=1'); | |
− | + | echo "</ul>"; | |
− | + | elseif ( is_single() ) { | |
− | + | // 投稿の固定リンクページを表示している際は何も表示させない | |
− | } elseif (is_page()) { | + | } elseif ( is_page() ) { |
− | + | // 固定ページを表示しています。どのページ? | |
− | + | if ( is_page( '管理人について' ) ) { | |
− | + | // 「管理人について」ページ | |
− | + | echo "<p>当ブログの管理人についての説明ページ。</p>"; | |
− | + | } elseif ( is_page( 'ブログについて' ) ) { | |
− | + | echo "<p>当ブログについての説明ページ。</p>"; | |
− | + | } else { | |
− | + | // その他の固定ページで表示させたいものがあればここに記入 | |
− | + | } | |
} else { | } else { | ||
− | + | // その他のページ(アーカイブページ、検索、404など)で表示させたいものがあればここに記入 | |
} // 以上で終わりです! | } // 以上で終わりです! | ||
?> | ?> | ||
472行目: | 548行目: | ||
<div> | <div> | ||
<input type="text" name="s" id="s" size="15" /> | <input type="text" name="s" id="s" size="15" /> | ||
− | <input type="submit" value="<?php _e('Search'); ?>" /> | + | <input type="submit" value="<?php _e( 'Search' ); ?>" /> |
</div> | </div> | ||
</form> | </form> | ||
484行目: | 560行目: | ||
=== 分かりやすい 404 ページ === | === 分かりやすい 404 ページ === | ||
</div> | </div> | ||
− | [[Creating an Error 404 Page]] | + | [[Creating an Error 404 Page]] という投稿の [[Creating an Error 404 Page#サーバーが 404 ページを見つけるのを助ける|サーバーが 404 ページを見つけるのを助ける]] というセクションに、PHP の条件分岐関数 <code>isset()</code> を使う例が書かれています。 |
<!-- リンク先のコード内のコメントも下記のように訳す。それまで参考としてコメントアウトで残しておきます。 | <!-- リンク先のコード内のコメントも下記のように訳す。それまで参考としてコメントアウトで残しておきます。 | ||
497行目: | 573行目: | ||
$websitename = get_bloginfo('name'); # WordPress サイトの名称 | $websitename = get_bloginfo('name'); # WordPress サイトの名称 | ||
− | if (!isset($_SERVER['HTTP_REFERER'])) { | + | if ( !isset($_SERVER['HTTP_REFERER']) ) { |
# 丁寧にユーザーエラーを指摘 :-) | # 丁寧にユーザーエラーを指摘 :-) | ||
echo "ご覧になろうとした"; # 出力する文章の組み立てを開始 | echo "ご覧になろうとした"; # 出力する文章の組み立てを開始 | ||
$casemessage = "でも、これが世界の終わりではありません!"; | $casemessage = "でも、これが世界の終わりではありません!"; | ||
− | } elseif (isset($_SERVER['HTTP_REFERER'])) { | + | } elseif ( isset($_SERVER['HTTP_REFERER'] ) ) { |
# 読者が探しているページを見つけるためのヒント。また、間違ったリンクを管理人に送信 | # 読者が探しているページを見つけるためのヒント。また、間違ったリンクを管理人に送信 | ||
echo "クリックされたリンク先の、 "; | echo "クリックされたリンク先の、 "; | ||
536行目: | 612行目: | ||
<?php | <?php | ||
wp_reset_query(); | wp_reset_query(); | ||
− | if (is_page('2') ) { | + | if ( is_page( '2' ) ) { |
− | echo ' | + | echo 'ページ 2 です。'; |
} | } | ||
?> | ?> | ||
547行目: | 623行目: | ||
<?php | <?php | ||
query_posts('posts_per_page=5&cat=123'); | query_posts('posts_per_page=5&cat=123'); | ||
− | if(have_posts()) : | + | if( have_posts() ) : |
− | while(have_posts()) : | + | while( have_posts() ) : |
the_posts(); | the_posts(); | ||
/* 投稿を出力するなどの処理... */ | /* 投稿を出力するなどの処理... */ | ||
588行目: | 664行目: | ||
* [[#The_Main_Page|is_home]] | * [[#The_Main_Page|is_home]] | ||
* [[#A_Date_Page|is_month]] | * [[#A_Date_Page|is_month]] | ||
+ | * [[#A Multi-author Site|is_multi_author]] | ||
+ | * [[#Part_of_a_network|is_multisite]] | ||
* [[#A_PAGE_Page|is_page]] | * [[#A_PAGE_Page|is_page]] | ||
* [[#Is_a_Page_Template|is_page_template]] | * [[#Is_a_Page_Template|is_page_template]] | ||
596行目: | 674行目: | ||
* [[#A_Single_Page,_Single_Post_or_Attachment|is_singular]] | * [[#A_Single_Page,_Single_Post_or_Attachment|is_singular]] | ||
* [[#A_Sticky_Post|is_sticky]] | * [[#A_Sticky_Post|is_sticky]] | ||
+ | * [[#Admin_of_a_network|is_super_admin]] | ||
* [[#A_Tag_Page|is_tag]] | * [[#A_Tag_Page|is_tag]] | ||
+ | * [[#A_Taxonomy_Page|is_tax]] | ||
* [[#A Taxonomy Page|is_tax]] | * [[#A Taxonomy Page|is_tax]] | ||
* [[#A_Date_Page|is_time]] | * [[#A_Date_Page|is_time]] | ||
622行目: | 702行目: | ||
** <code>is_attachment()</code> を追加。 | ** <code>is_attachment()</code> を追加。 | ||
− | {{原文|Conditional Tags| | + | {{原文|Conditional Tags|111270}}<!-- 2011-11-28T22:11:30 Jeffsebring 版 --> |
==関数リファレンス== | ==関数リファレンス== | ||
642行目: | 722行目: | ||
[[en:Conditional Tags]] | [[en:Conditional Tags]] | ||
[[fr:Marqueurs conditionnels]] | [[fr:Marqueurs conditionnels]] | ||
+ | [[pt-br:Tags condicionais]] | ||
[[tr:Koşul Etiketleri]] | [[tr:Koşul Etiketleri]] | ||
[[zh-hans:条件标签]] | [[zh-hans:条件标签]] |
2011年12月27日 (火) 12:27時点における版
このページ「条件分岐タグ」は一部未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています。
目次
- 1 はじめに
- 2 条件の判定
- 2.1 メインページ
- 2.2 フロントページ
- 2.3 管理パネル
- 2.4 個別投稿ページ
- 2.5 先頭固定表示の投稿
- 2.6 投稿タイプ
- 2.7 階層を持つ投稿タイプ
- 2.8 投稿タイプアーカイブ
- 2.9 コメントポップアップ
- 2.10 投稿を含むページ
- 2.11 WordPress ページ
- 2.12 カテゴリーページ
- 2.13 タグページ
- 2.14 タクソノミーページ
- 2.15 登録タクソノミー
- 2.16 作成者ページ
- 2.17 作成者ページ
- 2.18 日付別ページ
- 2.19 アーカイブページ
- 2.20 検索結果ページ
- 2.21 404 Not Found ページ
- 2.22 複数にわたるページ
- 2.23 添付ファイルページ
- 2.24 シングルページ (固定ページ、個別投稿ページ、添付ファイルページ)
- 2.25 フィード
- 2.26 トラックバック
- 2.27 プレビュー
- 2.28 抜粋あり
- 2.29 その他の使い方
- 2.30 ナビゲーションメニュー割り当てあり
- 2.31 ループ内
- 2.32 サイドバー使用中
- 2.33 ネットワークの一部(マルチサイト)
- 2.34 ネットワークの管理者(マルチサイト)
- 2.35 有効化済みプラグイン
- 2.36 機能対応のテーマ
- 3 動作サンプル
- 4 条件分岐タグ インデックス
- 5 変更履歴
- 6 関数リファレンス
はじめに
条件分岐タグ (Conditional Tag) は、テンプレートファイル内で表示される内容や、特定のページ内容を表示する条件を設定するのに使います。例えば、ブログのホームページの上部に短い文を表示させたいとしましょう。is_home()を使えば、簡単に実行できます。
これらのタグはテンプレート階層と深い関わりがあります。
注: 条件分岐タグが使えるのは WordPress の init アクションフックの後のみです。テーマの function.php 本文内(関数の外)では正しく動作しません。
条件の判定
条件分岐タグは、現在のページが指定した条件に合致するかどうかをチェックし、TRUE もしくは FALSE を返します。以下の条件分岐タグの説明では、TRUE 値が返される場合の説明をしています。また、タグで引数が使える場合は追記してあります。
メインページ
- is_home()
- メインブログページが表示されている場合。
- This is the page which is showing the time based blog content of your site, so if you've set a static Page for the Front Page (see below), then this will only be true on the Page which you set as the "Posts page" in Administration > Settings > Reading.
フロントページ
- is_front_page()
- サイトのフロントページが表示されている場合(投稿・固定ページにかかわらず)。
- true を返すのは、表示設定の「トップページの表示」として
- 「最新の投稿」を設定している場合には、メインブログページが表示されるとき
- 「固定ページ (以下を選択)」を設定している場合には、「フロントページ」として選択した固定ページが表示されるとき
管理パネル
- is_admin()
- ダッシュボードまたは管理パネルが表示されている場合。
個別投稿ページ
- is_single()
- 個別投稿のページ(または添付ファイルページ・カスタム投稿タイプの個別ページ)が表示されている場合。固定ページには適用されない。
- is_single( '17' )
- ID 17の投稿が表示されている場合。
- is_single( 'Irish Stew' )
- "Irish Stew" というタイトルの投稿が表示されている場合。
- is_single( 'beef-stew' )
- "beef-stew" という投稿スラッグの投稿が表示されている場合。
- is_single( array( 17, 'beef-stew', 'Irish Stew' ) )
- ID が 17、投稿スラッグが "beef-stew"、またはタイトルが "Irish Stew" のいずれかにあてはまる投稿が表示されている場合。
- is_single( array( 17, 19, 1, 11 ) )
- Returns true when the single post being displayed is either post ID 17, post ID 19, post ID 1, or post ID 11.
- is_single( array( 'beef-stew', 'pea-soup', 'chili' ) )
- Returns true when the single post being displayed is either the post_name "beef-stew", post_name "pea-soup" or post_name "chili".
- is_single( array( 'Beef Stew', 'Pea Soup', 'Chili' ) )
- Returns true when the single post being displayed is either the post_title "Beef Stew", post_title "Pea Soup" or post_title "Chili".
注: この関数は投稿 ID、投稿タイトル、または投稿名を区別しません。投稿 ID が「17」の投稿をリクエストした場合、タイトルや投稿スラッグが「17」の投稿が表示されることがあります。
先頭固定表示の投稿
- is_sticky()
- 投稿編集ページで「この投稿を先頭に固定表示」のチェックボックスがついている投稿が表示されている場合。もし ID 変数が指定されていない場合、投稿 ID は WordPress ループの投稿のものになる。
- is_sticky( '17' )
- ID 17 の投稿が先頭固定表示の場合、真を返す。
投稿タイプ
- get_post_type()
- Not really a conditional tag, but returns the registered post type of the current post.
- if( 'book' == get_post_type() ) ...
- Tests to see if the current post is of type 'book'.
- post_type_exists()
- Returns true if a given post type is a registered post type. This does not test if a post is a certain post_type. Note: This function replaces a function called is_post_type which existed briefly in 3.0 development.
階層を持つ投稿タイプ
- is_post_type_hierarchical($post_type)
- Returns true if this $post_type has been set with hierarchical support when registered.
- is_post_type_hierarchical( 'book' )
- Returns true if the book post type was registered as having support for hierarchical.
投稿タイプアーカイブ
- is_post_type_archive() : Returns true on any post type archive.
- is_post_type_archive( $post_type ) : Returns true if on a post type archive page that matches $post_type (can be a single post type or an array of post types).
To turn on post type archives, use 'has_archive' => true, when registering the post type.
コメントポップアップ
- is_comments_popup()
- コメントポップアップウィンドウが表示されている場合。
投稿を含むページ
- comments_open()
- WordPress ループ内で処理中の投稿がコメント受信を受け付けている場合。
- pings_open()
- WordPress ループ内で処理中の投稿がピン(ピンバックおよびトラックバック)を受け付けている場合。
WordPress ページ
ここでいう「ページ」とは、WordPressにおける固定ページのことで、いわゆる「Web ページ」とは異なります。つまり、post_type が 'page' となっているもののことです。詳しくは、「用語について」をご覧ください。
- is_page()
- 固定ページが表示されている場合。
- is_page( '42' )
- ID 42の固定ページが表示されている場合。
- is_page( 'About Me And Joe' )
- "About Me And Joe"というタイトルの固定ページが表示されている場合。
- is_page( 'about-me' )
- "about-me"という投稿スラッグの固定ページが表示されている場合。
- is_page( array( 42, 'about-me', 'About Me And Joe' ) )
- ID が 42、投稿スラッグが "about-me"、またはタイトルが "About Me And Joe" のいずれかにあてはまる固定ページが表示されている場合。
- is_page( array( 42, 54, 6 ) )
- ID が 42、54、または6のいずれかの固定ページが表示されている場合。
複数にわたるページの一部かどうかをチェックする
You can use this code to check whether you're on the nth page in a Post or PAGE Page that has been divided into pages using the <!--nextpage--> QuickTag. This can be useful, for example, if you wish to display meta-data only on the first page of a post divided into several pages.
例 1
<?php $paged = $wp_query->get( 'paged' ); if ( ! $paged || $paged < 2 ) { // 複数にわたるページの一部ではない場合 (または複数にわたる投稿・固定ページの最初のページの場合) } else { // 複数にわたるページの一部である場合 } ?>
例 2
<?php $paged = get_query_var( 'page' ) ? get_query_var( 'page' ) : false; if ( $paged === false ) { // 複数ページの一部ではない場合 (または投稿・固定ページの複数ページの最初のページの場合) } else { // 複数ページの一部である場合 } ?>
サブページかどうかをチェックする
残念ながら、is_subpage()というタグは今のところありませんが、対応策はあります。
スニペット 1
<?php global $post; // ループ外の場合 if ( is_page() && $post->post_parent ) { // サブページの場合 } else { // サブページではない場合 } ?>
以下のように is_subpage() 関数を作ることもできます。functions.php ファイルにこの関数を追加すればスニペット 1 のようにサブページかどうかをチェックすることができます。この関数は現在のページがサブページなら親ページの ID を、サブページでないなら false を返します。
スニペット 2
function is_subpage() { global $post; // $post には現在の固定ページの情報があります if ( is_page() && $post->post_parent ) { // 現在の固定ページが親ページを持つかどうかをチェックします return $post->post_parent; // 親ページの ID を返します } else { // 親ページを持たないので... return false; // ...false を返します }; };
サブページかどうかのチェックを頻繁にする予定なら、スニペット 1 よりも スニペット 2 のように関数にして利用する方が便利です。
現在のページの親ページが特定の固定ページ、例えば "紹介"(デフォルトでページ ID は 2)かどうかをチェックするならスニペット 3 のようにチェックできます。この方法は、指定したページであるか、またはそのサブページであるかをチェックします。サイトのセクションごとに異なった変数(バナーや見出しなど)を設定するのに便利です。
スニペット 3
<?php if ( is_page( 'about' ) || '2' == $post->post_parent ) { // このページは "紹介"、または親ページが "紹介" $bannerimg = 'about.jpg'; } elseif ( is_page( 'learning' ) || '56' == $post->post_parent ) { $bannerimg = 'teaching.jpg'; } elseif ( is_page( 'admissions' ) || '15' == $post->post_parent ) { $bannerimg = 'admissions.jpg'; } else { $bannerimg = 'home.jpg'; // 上のどれにも当てはまらないページ(たぶんホームページ) } ?>
スニペット 4 はスニペット 3 のチェックをより簡単に実現する関数です。この関数は指定したページ('紹介' など)またはそのサブページ(親ページの ID が 2)であれば true を返します。
スニペット 4
function is_tree( $pid ) { // $pid = 指定したページの ID global $post; // $post に現在の固定ページの情報をロード if ( is_page($pid) ) return true; // その固定ページまたはサブページの場合 $anc = get_post_ancestors( $post->ID ); foreach ( $anc as $ancestor ) { if( is_page() && $ancestor == $pid ) { return true; } } return false; // その固定ページではない、または親ページではない場合 }
スニペット 4 を functions.php ファイルに追加して、is_tree( 'id' ) を実行すると現在のページが指定したページ、またはそのサブページであるかどうかがわかります。例えば、スニペット 3 の始めの if ブロック内にある "is_page( 'about' ) || '2' == $post->post_parent" は is_tree( '2' ) に置き換えられます。
注意:2階層以上のページ階層がある場合は、親ページは現在より1階層上のページを差します。ページ階層の最上にあるページではないことに注意してください。
ページテンプレート
この条件分岐タグを使うと、特定のページテンプレートを使っているかどうかをチェックできます。
- is_page_template()
- ページテンプレートが使われている場合。
- is_page_template('about.php')
- "about" というページテンプレートが使われている場合。他の条件分岐タグとは違い、特定のテンプレートを選びたい場合には about.php などのファイル名を指定する必要があります。
カテゴリーページ
- is_category()
- あるカテゴリーのアーカイブページが表示されている場合。
- is_category( '9' )
- カテゴリーID 9のアーカイブページが表示されている場合。
- is_category( 'Stinky Cheeses' )
- "Stinky Cheeses"というカテゴリーのアーカイブページが表示されている場合。
- is_category( 'blue-cheese' )
- "blue-cheese"というカテゴリースラッグのアーカイブページが表示されている場合。
- is_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) )
- 投稿のカテゴリーが term_ID 9、スラッグが "blue-cheese" の場合、カテゴリー名が "Stinky Cheeses" のいずれかにあてはまる場合、真を返す。
- in_category( '5' )
- (注: in_category です。) 現在の投稿がカテゴリーID 5に属する場合にtrueを返します。 詳細
- in_category( array( 1,2,3 ) )
- Returns true if the current post is in either category 1, 2, or 3.
- ! in_category( array( 4,5,6 ) )
- Returns true if the current post is NOT in either category 4, 5, or 6. Note the ! at the beginning.
is_archive() とカテゴリーテンプレートも参照してください。
タグページ
- is_tag()
- タグのアーカイブページが表示されている場合。
- is_tag( 'mild' )
- 'mild' というスラッグのついたタグのアーカイブページが表示されている場合。
- is_tag( array( 'sharp', 'mild', 'extreme' ) )
- 'sharp' または 'mild' または 'extreme' というスラッグのついたタグのアーカイブページが表示されている場合。
- has_tag()
- タグのある投稿がループ内で表示されている場合。
- has_tag( 'mild' )
- 'mild' というタグのある投稿が表示されている場合。
- has_tag( array( 'sharp', 'mild', 'extreme' ) )
- 配列内のいずれかのタグのある投稿が表示されている場合。
is_archive() と タグテンプレート/en もあわせてお読みください。
タクソノミーページ
- is_tax()
- タクソノミーのアーカイブページが表示されている場合。
- is_tax( 'flavor' )
- 'flavor' というスラッグのついたタクソノミーのアーカイブページが表示されている場合。
- is_tax( 'flavor', array( 'sharp', 'mild', 'extreme' ) )
- 'sharp' または 'mild' または 'extreme' というスラッグのついた flavor タクソノミーのアーカイブページが表示されている場合。
is_archive() もあわせてお読みください。
登録タクソノミー
- taxonomy_exists()
- あるタクソノミーが register_taxonomy() を使って登録されている場合。バージョン 3.0 で非推奨になるまでは is_taxonomy() と呼ばれていた。
作成者ページ
- is_author()
- 作成者のアーカイブページが表示されている場合。
- is_author( '4' )
- ID 4 の作成者のアーカイブページを表示している場合。
- is_author( 'Vivian' )
- ニックネームが"Vivian"のアーカイブページを表示している場合。
- is_author( 'john-jones' )
- "john-jones" という「ナイスネーム」を持つ作成者のアーカイブページを表示している場合。
- is_author( array( 4, 'john-jones' , 'Vivian' ) )
- ID が4、ナイスネームが "john-jones"、またはニックネームが "Vivian" という作成者のアーカイブページを表示している場合。
is_archive() や作成者テンプレート/en もあわせてお読みください。
- is_multi_author()
- サイト上に一人以上投稿を公開しているユーザーがいる場合。バージョン 3.2 から導入された。
is_archive() や作成者テンプレート/en もあわせてお読みください。
日付別ページ
- is_date()
- 日付別のアーカイブページのいずれかが表示されている場合。(例:月別、年別、日別、時間別)
- is_year()
- 年別のアーカイブページが表示されている場合。
- is_month()
- 月別のアーカイブページが表示されている場合。
- is_day()
- 日別のアーカイブページが表示されている場合。
- is_time()
- 毎時別、毎分別、毎秒別のアーカイブページが表示されている場合。
- is_new_day()
- 投稿の日付が新しい日の場合。ループ内で使う。
is_archive() もあわせてお読みください。
アーカイブページ
- is_archive()
- 各アーカイブページが表示されている場合。アーカイブページには、カテゴリー、タグ、作成者、日付別のものがあります。
検索結果ページ
- is_search()
- 検索結果のページが表示されている場合。
404 Not Found ページ
- is_404()
- "HTTP 404: Not Found"エラーページが表示されている場合。
複数にわたるページ
- is_paged()
- 表示中のページが複数のページにわたる場合。これは例えばアーカイブページやメインページに表示する投稿数よりも多い投稿があり、複数ページに分かれているときを指し、2ページ目以降のときに true を返します。ただし、1つの投稿や固定ページの本文が <!--nextpage--> クイックタグで複数ページに分けられている場合は当てはまりません。投稿または固定ページが <!--nextpage--> クイックタグを使って分割されているかどうか調べるには、複数にわたるページの一部かどうかをチェックするをご覧ください。
添付ファイルページ
- is_attachment()
- 投稿または固定ページの添付ファイルが表示されている場合。添付ファイルは、投稿画面のメディアアップロード機能を使ってアップロードした画像などのファイルの事で、テンプレートを使って独自のページ内に表示できます。詳細は、 画像や添付ファイルの使い方/en をご覧ください。
シングルページ (固定ページ、個別投稿ページ、添付ファイルページ)
- is_singular()
- is_single()、is_page() 、is_attachment() のいずれかが真である場合。
- is_singular( 'book' )
- 'book' というカスタム投稿タイプの投稿を表示している場合。
- is_singular( array( 'newspaper', 'book' ) )
- 'newspaper' または 'book' というカスタム投稿タイプの投稿を表示している場合。
フィード
- is_feed()
- Syndication(フィード)がリクエストされた場合。
- このタグは通常、一般のユーザーがページを表示しているときに条件を判断するためではなく、プラグイン開発の際に WordPress 内部で使われます。
トラックバック
- is_trackback()
- フックの中にトラックバックの機構が組み込まれている場合。
- このタグは通常、一般のユーザーがページを表示しているときに条件を判断するためではなく、プラグイン開発の際に WordPress 内部で使われます。
プレビュー
- is_preview()
- 未公開モードで固定リンクページを表示している場合。
抜粋あり
- has_excerpt()
- 投稿に (手動で書かれた) 抜粋がある場合。
- has_excerpt( '42' )
- 投稿 ID 42 の投稿に抜粋がある場合。
<?php // もし関数内なら $post を取得 global $post; if ( empty( $post->post_excerpt ) ) { // 投稿に抜粋がない場合 } else { // 投稿に抜粋がある場合 } ?>
その他の使い方
when you need hide the excerpt auto displayed and only display yours posts excerpts.
<?php if ( ! has_excerpt() ) { echo ''; } else { the_excerpt(); }
Replace auto excerpt for a text or code.
<?php if ( ! has_excerpt() ) {?> <!-- you text or code --> <?php } ?>
- has_nav_menu()
- 登録済みのナビゲーションメニュー一が割り当てられているかどうかをチェックする。割り当て済み (true) または割り当てなし (false) を返す。
ループ内
- in_the_loop()
- ループ内かどうかをチェックする。ループ内にあった場合真を返すので、プラグイン作者に便利。
サイドバー使用中
- is_active_sidebar()
- 指定したダイナミックサイドバーが使用中の場合、真を返す。サイドバー名、ID、番号を指定できる。
ネットワークの一部(マルチサイト)
- is_multisite is_multisite()
- 現在のサイトがマルチサイトインストールの一部かどうかチェックする。
ネットワークの管理者(マルチサイト)
- is_super_admin()
- ユーザーがネットワークの特権管理者かどうかをチェックする。
有効化済みプラグイン
- is_plugin_active()
- プラグインが有効化されているかチェックする。
機能対応のテーマ
- current_theme_supports()
- テーマに機能が存在するかチェックする。
動作サンプル
以下は条件分岐タグの動作サンプルです。
個別投稿
以下はis_single()によって特定の情報を個別投稿のページでのみ表示させるための例文です。
if ( is_single() ) { echo 'この投稿は' . single_cat_title() . 'カテゴリーに属しています'; }
以下の例は、現在表示しているページによって本文を表示するか抜粋を表示するかを切り替えます。
if ( is_home() || is_single() ) { the_content(); } else { the_excerpt(); }
日付別の表示
以下の例は、日付別ページへのアクセスが行われたときに、年別に色分けして表示させます。
<?php // WordPress ループを開始 if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <h2 id="post-<?php the_ID(); ?>"> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"> <?php the_title(); ?></a></h2> <small><?php the_time('Y/n/j') ?> <!-- by <?php the_author() ?> --></small> <?php // 日付別アーカイブページかどうかをチェック if ( is_date() ) { if ( date( 'Y' ) != get_the_date( 'Y' ) ) { // 今年の投稿ではない場合、"oldentry" クラスを追加してスタイルを変える echo '<div class="oldentry">'; } else { echo '<div class="entry">'; } } else { echo '<div class="entry">'; } the_content( '続きを読む »' ); ?> </div>
サイドバーの内容をページによって変更する
以下の例では、閲覧中のページによってサイドバーに表示させる内容を変更します。
(注)下記コード内の wp_list_cats は廃止予定です。替わりにwp_list_categories をお使いください。(コードを直してくれる人募集中!)
<!-- サイドバー開始 --> <div id="sidebar"> <?php // 表示中のページに合わせたサイドバーコンテンツを生成 if ( is_home() ) { // ホームページを表示している際、トップレベルカテゴリーの一覧を表示 echo "<ul>"; wp_list_cats('optionall=0&sort_column=name&list=1&children=0'); echo "</ul>"; } elseif ( is_category() ) { // カテゴリーアーカイブページを表示している際、そのカテゴリー内のサブカテゴリーを含む一覧を表示 echo "<ul>"; wp_list_cats('optionall=1&sort_column=name&list=1&children=1&hierarchical=1'); echo "</ul>"; elseif ( is_single() ) { // 投稿の固定リンクページを表示している際は何も表示させない } elseif ( is_page() ) { // 固定ページを表示しています。どのページ? if ( is_page( '管理人について' ) ) { // 「管理人について」ページ echo "<p>当ブログの管理人についての説明ページ。</p>"; } elseif ( is_page( 'ブログについて' ) ) { echo "<p>当ブログについての説明ページ。</p>"; } else { // その他の固定ページで表示させたいものがあればここに記入 } } else { // その他のページ(アーカイブページ、検索、404など)で表示させたいものがあればここに記入 } // 以上で終わりです! ?> <form id="searchform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <div> <input type="text" name="s" id="s" size="15" /> <input type="submit" value="<?php _e( 'Search' ); ?>" /> </div> </form> </div> <!-- end sidebar -->
分かりやすい 404 ページ
Creating an Error 404 Page という投稿の サーバーが 404 ページを見つけるのを助ける というセクションに、PHP の条件分岐関数 isset()
を使う例が書かれています。
ダイナミックメニューハイライト
ダイナミックハイライトとは、条件分岐タグを使用して、メニュー内の現在アクセスしているページを際立たせる方法です。
sidebar.php などの他のテンプレートファイルによってクエリが実行された場合、元の条件分岐タグは破損してしまいます。例えば、header.php では条件分岐タグが適切に機能するのに、footer.php では正しく機能しないトラブルがよくあります。これを解決するにはフッターで条件分岐タグを使う前に wp_reset_query を実行します:
<?php wp_reset_query(); if ( is_page( '2' ) ) { echo 'ページ 2 です。'; } ?>
このようなトラブルを無くすために、別の場所(テンプレートファイルやプラグイン)でクエリを実行した場合は、必要な処理を終えた後に必ず wp_reset_query を実行するようにしましょう。
<?php query_posts('posts_per_page=5&cat=123'); if( have_posts() ) : while( have_posts() ) : the_posts(); /* 投稿を出力するなどの処理... */ endwhile; else : /* 該当する投稿がない場合、何もしない */ endif; wp_reset_query(); ?>