WordPress 条件タグ【Conditional Tags】解説

Sponsored Link

条件タグは、どのような条件がページが一致するかどうかに応じて、コンテンツがどのように表示し、コンテンツがどのように特定のページに表示するかを、変更するためにテンプレートファイルを使用する事ができます。

例えば、ブログのメインページだけに、一連の記事上のテキストの抜粋を表示したい場合があります。is_home()条件タグによって、タスクは簡単に作れます。

Sponsored Link

原文(翻訳元)The Conditional Tags can be used in your Template files to change what content is displayed and how that content is displayed on a particular page depending on what conditions that page matches. For example, you might want to display a snippet of text above the series of posts, but only on the main page of your blog. With the is_home() Conditional Tag, that task is made easy.

注:これらのタグは、WordPressのテンプレート階層と密接な関係にあります。

原文(翻訳元)Note: the close relation these tags have to WordPress Template Hierarchy.

条件の為に…

全ての条件タグは、 特定の条件が満たされるであろうとなかろうと参照するために分析し、その時TRUEかFALSEのいずれかを返します。条件下で、様々なタグがTRUEを出力した場合は、以下に記載されています。パラメーターが許可する事ができる、それらのタグが書かれています。

原文(翻訳元)All of the Conditional Tags test to see whether a certain condition is met, and then returns either TRUE or FALSE. The conditions under which various tags output TRUE is listed below. Those tags which can accept parameters are so noted.

メインページ

is_home()

メインブログのージが表示されている場合です。これは、サイトコンテンツのブログの時間を基づいて表示しているページです。もしフロントページ(下記参照)の静的ページを設定している場合は、管理画面>設定>表示設定内の”フロントページの表示”として設定しているページがTrueの場合のみです。

原文(翻訳元)When the main blog page is being displayed. 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を返します。注:このタグはバージョン2.5で追加されました。

原文(翻訳元)When it is the front of the site displayed, whether it is posts or a Page. Returns true when the main blog page is being displayed and the ‘Settings > Reading ->Front page displays’ is set to “Your latest posts”, or when ‘Settings > Reading ->Front page displays’ is set to “A static page” and the “Front Page” value is the current Page being displayed. Note: this tag was added at Version 2.5.

管理者パネル

is_admin()

ダッシュボードか管理パネルが表示されてる場合です。

原文(翻訳元)When the Dashboard or the administration panels are being displayed.

シングルの投稿ページ

is_single()

シングル投稿ページが表示されている場合です。

原文(翻訳元)When any single Post page is being displayed.

is_single(’17’)

投稿17がシングル投稿として表示されている場合です。

原文(翻訳元)When Post 17 is being displayed as a single Post.

is_single(‘Irish Stew’)

“Irish Stew”タイトルの投稿がシングル投稿として表示されている場合です。

原文(翻訳元)When the Post with Title “Irish Stew” is being displayed as a single Post.

is_single(‘beef-stew’)

投稿スラッグ”beef-stew”の投稿がシングル投稿として表示されている場合です。

原文(翻訳元)When the Post with Post Slug “beef-stew” is being displayed as a single Post.

is_single(array(17,’beef-stew’,’Irish Stew’))

シングル投稿が投稿ID17、post_nameが”beef-stew”、post_titleが”Irish Stew”のいずれか表示されている時にtrueを返します。

原文(翻訳元)Returns true when the single post being displayed is either post ID 17, or the post_name is “beef-stew”, or the post_title is “Irish Stew”.

is_single(array(17, 19, 1, 11))

シングル投稿が記事ID17、記事ID19、記事ID1、記事ID11のいずれか表示された時にtrueを返します。

原文(翻訳元)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’))

シングル投稿が、post_name”beef-stew”、post_name”pea-soup”、post_name”chili”のいずれか表示された時にtrueを返します。

原文(翻訳元)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’))

投稿記事がpost_title “Beef Stew”、post_title “Pea Soup”、post_title “Chili”のいずれか表示された時にtrueを返します。

原文(翻訳元)Returns true when the single post being displayed is either the post_title “Beef Stew”, post_title “Pea Soup” or post_title “Chili”.

注:配列の機能はバージョン2.5で追加されました。この関数は、投稿ID名、投稿タイトル、投稿名の間に区別されません。”17″という名付けられた投稿は、もし投稿IDの17が要求された場合に表示されます。恐らく、スラッグ”17″と投稿の保持する事と同じです。

原文(翻訳元)Note: The array ability was added at Version 2.5. This function does not distinguish between the post ID, post title, or post name. A post named “17” would be displayed if a post ID of 17 was requested. Presumably the same holds for a post with the slug “17”.

固定投稿

is_sticky()

“この投稿をフロントページに固定する”チェックボックスが、現在の投稿にチェックされていた場合にtrueを返します。この例では、投稿IDの引数が与えられないので、投稿IDのループ投稿のように使用されます。注:このタグは、バージョン2.7から追加されました。

原文(翻訳元)Returns true if “Stick this post to the front page” check box has been checked for the current post. In this example, no post ID argument is given, so the post ID for the Loop post is used. Note: this tag was added at Version 2.7.

is_sticky(’17’)

投稿17が固定投稿と見なされた時にtrueを返します。

原文(翻訳元)Returns true when Post 17 is considered a sticky post.

投稿タイプ

get_post_type()

条件タグでないが、現在の投稿の登録された投稿タイプを返します。

原文(翻訳元)Not really a conditional tag, but returns the registered post type of the current post.

if( get_post_type() == ‘book’ ) …

現在の投稿が’book’タイプの場合、確認をテストします。

原文(翻訳元)Tests to see if the current post is of type ‘book’.

post_type_exists()

投稿タイプが与えられ、投稿タイプを登録された場合、trueを返します。これは投稿が特定のpost_typeの場合、テストしていません。注:この関数は、3.0の開発で簡単に存在しているis_post_type関数に置き換えられます。

原文(翻訳元)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)

この$post_typeが登録された階層をサポートする設定をされていた場合、trueを返します。注:この機能は、バージョン3.0で追加されました。

原文(翻訳元)Returns true if this $post_type has been set with hierarchical support when registered. Note: This ability was added at Version 3.0.

is_post_type_hierarchical(‘book’)

book投稿タイプが階層のサポートを持つとして登録された場合、trueを返します。

原文(翻訳元)Returns true if the book post type was registered as having support for hierarchical.

コメントポップアップ

is_comments_popup()

コメントポップアップウィンドウの時

原文(翻訳元)When in Comments Popup window.

すべての投稿を含むページ

comments_open()

コメントがWordPressのループ内で処理された現在の投稿を許可する場合です。

原文(翻訳元)When comments are allowed for the current Post being processed in the WordPress Loop.

pings_open()

ピングがWordPressのループ内で処理された現在の投稿を許可する場合です。

原文(翻訳元)When pings are allowed for the current Post being processed in the WordPress Loop.

PAGEページ

ブログから任意の汎用ウェブページではなく、WordPressのページを参照したセクションです。

原文(翻訳元)This section refers to WordPress Pages, not any generic webpage from your blog.

is_page()

全てのページが表示された場合です。

原文(翻訳元)When any Page is being displayed.

is_page(’42’)

Page42(ID)が表示されている場合です。

原文(翻訳元)When Page 42 (ID) is being displayed.

is_page(‘About Me And Joe’)

“About Me And Joe”のページタイトルでページが表示されている場合です。

原文(翻訳元)When the Page with a post_title of “About Me And Joe” is being displayed.

is_page(‘about-me’)

“about-me”のページ名(スラッグ)のページで表示された場合です。

原文(翻訳元)When the Page with a post_name (slug) of “about-me” is being displayed.

is_page(array(42,’about-me’,’About Me And Joe’))

投稿ID42、投稿名が”about-me”、投稿タイトルが”About Me And Joe”のいずれかで表示されたページの時にtrueを返します。注:配列の機能はバージョン2.5で追加されました。

原文(翻訳元)Returns true when the Pages displayed is either post ID 42, or post_name “about-me”, or post_title “About Me And Joe”. Note: the array ability was added at Version 2.5.

サブページをテストする

is_subpage()機能がまだないのですが、少ないコードでこれをテストする事ができます。

原文(翻訳元)
There is no is_subpage() function yet, but you can test this with a little code:

スニペット 1
<?php

global $post;     // if outside the loop

if ( is_page() && $post->post_parent ) {
    // This is a subpage

} else {
    // This is not a subpage
}
?>

スニペット2 のコードを使用して独自のis_subpage()関数を作成することができます。
functions.php ファイルに追加します。スニペット1と同じ方法に親ページのテストしますが、
1つある場合はページの親IDを返します。あるいは、ない場合はfalseを返します。

原文(翻訳元)You can create your own is_subpage() function using the code in Snippet 2. Add it to your functions.php file. It tests for a parent page in the same way as Snippet 1, but will return the ID of the page parent if there is one, or false if there isn’t.

スニペット 2
function is_subpage() {
	global $post;                                 // load details about this page
        if ( is_page() && $post->post_parent ) {      // test to see if the page has a parent
               $parentID = $post->post_parent;        // the ID of the parent is this
               return $parentID;                      // return the ID
        } else {                                      // there is no parent so...
               return false;                          // ...the answer to the question is false
        };
};

サブページに頻繁にテストする場合、スペニット1のようなシンプルなテストで使用するよりむしろ、スペニット2のような関数を使用する事をお勧めします。

原文(翻訳元)
It is advisable to use a function like that in Snippet 2, rather than using the simple test like Snippet 1, if you plan to test for sub pages frequently.

ページの親が特定のページである場合テストすることです、例えば”About”(デフォルトではページIDが2)、スぺニット3でテストで使用する事ができます。
任意の子ページを見ている場合と同様、質問ページを見ている場合、参照するためのチェックをテストします。別バナー画像や別ヘッダーのように、これはWebサイトの変えられる特定の別セクションの設定に便利です。

原文(翻訳元)To test if the parent of a page is a specific page, for instance “About” (page id pid 2 by default), we can use the tests in Snippet 3. These tests check to see if we are looking at the page in question, as well as if we are looking at any child pages. This is useful for setting variables specific to different sections of a web site, so a different banner image, or a different heading.

スニペット 3
<?php

if ( is_page('about') || $post->post_parent == '2' ) {    
    // the page is "About", or the parent of the page is "About"
    $bannerimg = 'about.jpg';

} elseif ( is_page('learning') || $post->post_parent == '56' ) {	
    $bannerimg = 'teaching.jpg';

} elseif ( is_page('admissions') || $post->post_parent == '15' ) { 
    $bannerimg = 'admissions.jpg';

} else { 
    $bannerimg = 'home.jpg'; // just in case we are at an unclassified page, perhaps the home page
}	

?>

スぺニット4は、より簡単に上記のテストを実行するための許可をする関数です。この関数は、サブページ(ID”2″の親ページのように)の1つ、あるいは、質問のページを見ている場合、trueを返します。

原文(翻訳元)Snippet 4 is a function that allows you to carry out the tests above more easily. This function will return true if we are looking at the page in question (so “About”) or one of its sub pages (so a page with a parent with ID “2”).

スニペット 4
function is_tree($pid) {      // $pid = The ID of the page we're looking for pages underneath
	global $post;         // load details about this page
	$anc = get_post_ancestors( $post->ID );
	foreach($anc as $ancestor) {
		if(is_page() && $ancestor == $pid) {
			return true;
		}
	}
	if(is_page()&&(is_page($pid))) 
               return true;   // we're at the page or at a sub page
	else 
               return false;  // we're elsewhere
};

スニペット4では、function.phpに追加して、現在のページがページの場合、あるいはページにサブページがある場合は参照するためにis_tree(‘id’) を呼び出します。スぺニット3では、is_tree(‘2’)は、タグの場合最初の内部に”is_page(‘about’) || $post->post_parent == ‘2’”を置き換えます。

原文(翻訳元)Add Snippet 4 to your functions.php file, and call is_tree(‘id’) to see if the current page is the page, or is a sub page of the page. In Snippet 3, is_tree(‘2’) would replace “is_page(‘about’) || $post->post_parent == ‘2’” inside the first if tag.

親ページに1つ以上のページのレベルがある場合は、ページは1つのディレクトリー上であり、階層の最上位の1つずつではありません。

原文(翻訳元)Note that if you have more than one level of pages the parent page is the one directly above and not the one at the very top of the hierarchy.

テンプレートページです

バージョン2.5以降では、ページテンプレート内出ないにしろ、もし特定のページテンプレートが使用されていた場合にしろ、決定するために判断します。

原文(翻訳元)Beginning with Version 2.5 this allows you to determine whether or not you are in a page template or if a specific page template is being used.

is_page_template()

ページテンプレートが使われていますか?

原文(翻訳元)Is a Page Template being used?

is_page_template(‘about.php’)

ページテンプレートは’about’が使用されていますか?about.phやmy_page_template.phpなど、ファイル名を使用する必要がある、特定のページテンプレートを指定したい場合は、他の条件とは異なる事に注意してください。

原文(翻訳元)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.

カテゴリーページ

is_category()

任意のカテゴリーアーカイブページが表示されている場合です。

原文(翻訳元)When any Category archive page is being displayed.

is_category(‘9’)

カテゴリー9のアーカイブページが表示されている場合です。

原文(翻訳元)When the archive page for Category 9 is being displayed.

is_category(‘Stinky Cheeses’)

“Stinky Cheeses”の名前のカテゴリーのアーカイブページが表示されている場合です。

原文(翻訳元)When the archive page for the Category with Name “Stinky Cheeses” is being displayed.

is_category(‘blue-cheese’)

カテゴリースラッグ”blue-cheese”のカテゴリーでアーカイブページが表示されている場合です。

原文(翻訳元)When the archive page for the Category with Category Slug “blue-cheese” is being displayed.

is_category(array(9,’blue-cheese’,’Stinky Cheeses’))

term_ID9、”blue-cheese”スラッグ、”Stinky Cheeses”の名前のいずれかで表示された投稿のカテゴリーの時にtrueを返します。注:配列機能は、バージョン2.5から追加されました。

原文(翻訳元)Returns true when the category of posts being displayed is either term_ID 9, or slug “blue-cheese”, or name “Stinky Cheeses”. Note: the array ability was added at Version 2.5.

in_category(‘5’)

現在の投稿が指定されたカテゴリーID内の場合、trueを返します。続きを読む。

原文(翻訳元)Returns true if the current post is in the specified category id. read more

in_category( array( 1,2,3 ) )

現在の投稿が、カテゴリー1、2、3のいずれかの中にある場合、trueを返します。

原文(翻訳元)Returns true if the current post is in either category 1, 2, or 3.

!in_category( array( 4,5,6 ) )

現在の投稿がカテゴリー4、5、6のいずれかでない場合、trueを返します。!で始まる場合は注意してください。

原文(翻訳元)Returns true if the current post is NOT in either category 4, 5, or 6. Note the ! at the beginning.

注:必ずテスト確認する時、スペルの確認をする事です。”is” と “in”は大きな違いです。

原文(翻訳元)Note: Be sure to check your spelling when testing, “is” and “in” are a big difference.

is_archive()とカテゴリーテンプレートを参照してください。

原文(翻訳元)See also is_archive() and Category Templates.

タグページ

is_tag()

全てのタグアーカイブページが表示されている場合です。

原文(翻訳元)When any Tag archive page is being displayed.

is_tag(‘mild’)

‘mild’のスラッグでタグのアーカイブページが表示されている場合です。

原文(翻訳元)When the archive page for tag with the slug of ‘mild’ is being displayed.

is_tag(array(‘sharp’,’mild’,’extreme’))

“sharp”, “mild”, “extreme”のいずれかのスラッグを持つタグアーカイブが表示されている時trueを返します。注:配列機能は、バージョン2.5から追加されました。

原文(翻訳元)Returns true when the tag archive being displayed has a slug of either “sharp”, “mild”, or “extreme”. Note: the array ability was added at Version 2.5.

has_tag()

現在の投稿タグの場合です。ループ内で使用しなければなりません。注:has_tagは、バージョン2.6から追加されました。

原文(翻訳元)When the current post has a tag. Must be used inside The Loop. Note: has_tag was added at Version 2.6.

has_tag(‘mild’)

現在の投稿が’mild’タグである場合です。

原文(翻訳元)When the current post has the tag ‘mild’.

has_tag(array(‘sharp’,’mild’,’extreme’))

現在の投稿が配列内の全てのタグである場合です。

原文(翻訳元)When the current post has any of the tags in the array.

is_archive()とタグテンプレートを参照してください。

原文(翻訳元)See also is_archive() and Tag Templates.

タクソノミーページ

is_tax()

全てのタクソノミーアーカイブページが表示されている場合です。

原文(翻訳元)When any Taxonomy archive page is being displayed.

is_tax(‘mild’)

‘mild’スラッグのタクソノミーのアーカイブページが表示されている場合です。

原文(翻訳元)When the archive page for taxonomy with the slug of ‘mild’ is being displayed.

is_tax(array(‘sharp’,’mild’,’extreme’))

“sharp”, “mild”, “extreme”のいずれかのスラッグを持つタクソノミーアーカイブが表示されている時trueを返します。

原文(翻訳元)Returns true when the taxonomy archive being displayed has a slug of either “sharp”, “mild”, or “extreme”.

is_archive()を参照してください。

原文(翻訳元)See also is_archive().

タクソノミーで登録された

is_taxonomy()

特定のタクソノミーが register_taxonomy()を介している場合です。

原文(翻訳元)When a particular taxonomy is registered via register_taxonomy().

投稿者ページ

is_author()

全ての投稿者ページを表示している場合です。

原文(翻訳元)When any Author page is being displayed.

is_author(‘4’)

投稿者ナンバー(ID)4のアーカイブページが表示されている場合です。

原文(翻訳元)When the archive page for Author number (ID) 4 is being displayed.

is_author(‘Vivian’)

“Vivian”のニックネームである投稿者のアーカイブページが表示されている場合です。

原文(翻訳元)When the archive page for the Author with Nickname “Vivian” is being displayed.

is_author(‘john-jones’)

“john-jones”のナイスネームである投稿者のアーカイブページが表示されている場合です。

原文(翻訳元)When the archive page for the Author with Nicename “john-jones” is being displayed.

is_author(array(4,’john-jones’,’Vivian’))

ユーザーID 4、user_nicenameが”john-jones”、ニックネームが”Vivian”のいずれかである投稿者のアーカイブページが表示されている場合です。注:配列機能は、バージョン2.5から追加されました。

原文(翻訳元)When the archive page for the author is either user ID 4, or user_nicename “john-jones”, or nickname “Vivian”. Note: the array ability was added at Version 2.5.

is_archive()や投稿者テンプレートを参照してください。

原文(翻訳元)See also is_archive() and Author Templates.

日付ページ

is_date()

全ての日付ベースのアーカイブページが表示されている場合です。(例. 月、年、日、時間ベースのアーカイブ)

原文(翻訳元)When any date-based archive page is being displayed (i.e. a monthly, yearly, daily or time-based archive).

is_year()

年アーカイブを表示している場合です。

原文(翻訳元)When a yearly archive is being displayed.

is_month()

月アーカイブを表示している場合です。

原文(翻訳元)When a monthly archive is being displayed.

is_day()

日アーカイブを表示している場合です。

原文(翻訳元)When a daily archive is being displayed.

is_time()

時間アーカイブを表示している場合です。

原文(翻訳元)When an hourly, “minutely”, or “secondly” archive is being displayed.

is_archive()を参照してください。

原文(翻訳元)See also is_archive().

すべてのアーカイブページ

is_archive()

全てのタイプのアーカイブページが表示されている場合です。カテゴリー、タグ、投稿者や日付ベースのページがアーカイブで全てのタイプです。

原文(翻訳元)When any type of Archive page is being displayed. Category, Tag, Author and Date based pages are all types of Archives.

検索結果ページ

is_search()

検索結果ページアーカイブが表示されている場合です。

原文(翻訳元)When a search result page archive is being displayed.

404 ページが見つかりません

is_404()

“HTTP 404: Not Found”エラーが発生した後に表示するページの場合です。

原文(翻訳元)When a page displays after an “HTTP 404: Not Found” error occurs.

ページングページ

is_paged()

表示されたページが”paged”である場合です。これは、アーカイブかメインページが複数のページ以上に分割されて、2番目にtrueを返し、記事の後続ページを参照します。これは、

Sponsored Link