WordPressの条件分岐タグまとめ
まずは、基本的なif文の使い方
<?php if(is_page('42')): ?> このページはページIDは"42"です! <?php elseif(is_page('About Me')): ?> このページのタイトルは"About Me"です! <?php elseif(is_page('about-me')): ?> このページの投稿スラッグは"about-me"です! <?php endif; ?> <?php if(is_home()): ?> <h1>AAAAAAAAAA</h1> <?php else: ?> <h1>BBBBBBBBBB</h1> <?php endif; ?>
WordPressの条件分岐タグのじぶんなりのまとめです。 スラッグに「index」が付いているページ
<?php if(is_page('index')): ?>~ <?php endif; ?>
メインページ
<?php if(is_home()): ?>~ <?php endif; ?>
フロントページ サイトのフロントページが表示されている場合。投稿の場合とページの場合あり。管理画面の「設定>表示設定>トップページの表示」で、「最新の投稿」が選択されているか、「固定ページ (以下を選択)」で現在のページが表示されている場合。 注: この条件タグは バージョン 2.5 で追加。
<?php if(is_front_page()): ?>~ <?php endif; ?>
シングルページ
<?php if(is_single()): ?>~ <?php endif; ?>
記事を含むページ
<?php if(comments_open()): ?>~ <?php endif; ?>
WordPress ループ内で処理中の記事がコメント受信を受け付けている場合
<?php if(pings_open()): ?>~ <?php endif; ?>
WordPress ループ内で処理中の記事がピン(ピンバックおよびトラックバック)を受け付けている場合 ページ
<?php if(is_page()): ?>~ <?php endif; ?>
ページテンプレート(バージョン 2.5以降)
<?php if(is_page_template()): ?>~ <?php endif; ?>
カテゴリーページ
<?php if(is_category()): ?>~ <?php endif; ?>
アーカイブページ ~ タグページ ~ 日付別ページ
<?php if(is_date()): ?>~ <?php endif; ?><?php if(is_year()): ?>~ <?php endif; ?><?php if(is_month()): ?>~ <?php endif; ?><?php if(is_day()): ?>~ <?php endif; ?><?php if(is_time()): ?>~ <?php endif; ?>
検索結果ページ
<?php if(is_search()): ?>~ <?php endif; ?>
404 Not Found ページ
<?php if(is_404()): ?>~ <?php endif; ?>
詳細条件分岐指定 シングルページ
<?php if(is_single('17')): ?>~ <?php endif; ?>
ID 17の記事が表示されている場合
<?php if(is_single('Irish Stew')): ?>~ <?php endif; ?>
“Irish Stew”(ビーフシチュー)というタイトルの記事が表示されている場合
<?php if(is_single('beef-stew')): ?>~ <?php endif; ?>
“beef-stew”(ビーフシチュー)という投稿スラッグの記事が表示されている場合
<?php if(is_single(array(17,'beef-stew','Irish Stew'))): ?>~ <?php endif; ?>
ID が 17、投稿スラッグが “beef-stew”、またはタイトルが “Irish Stew” のいずれかにあてはまる記事が表示されている場合。注: 配列を引数に使えるのは バージョン 2.5 以降 ページ
<?php if(is_page('42')): ?>~ <?php endif; ?>
ID 42のページが表示されている場合
<?php if(is_page('About Me And Joe')): ?>~ <?php endif; ?>
“About Me And Joe”というタイトルのページが表示されている場合
<?php if(is_page('about-me')): ?>~ <?php endif; ?>
“about-me”という投稿スラッグのページが表示されている場合
<?php if(is_page(array(42,'about-me','About Me And Joe'))): ?>~ <?php endif; ?>
ID が 42、投稿スラッグが”about-me”またはタイトルが”About Me And Joe”のいずれかにあてはまるページが表示されている場合 注: 配列を引数に使えるのは バージョン 2.5 以降 ページテンプレート(バージョン 2.5以降)
<?php if(is_page_template('about')): ?>~ <?php endif; ?>
“about”というページテンプレートが使われている場合 カテゴリーページ
<?php if(is_category('9')): ?>~ <?php endif; ?>
カテゴリーID 9のアーカイブページが表示されている場合
<?php if(is_category('Stinky Cheeses')): ?>~ <?php endif; ?>
“Stinky Cheeses”というカテゴリーのアーカイブページが表示されている場合
<?php if(is_category('blue-cheese')): ?>~ <?php endif; ?>
“blue-cheese”というカテゴリースラッグのアーカイブページが表示されている場合
<?php if(in_category('5')): ?>~ <?php endif; ?>
現在の記事がカテゴリーID 5に属する場合にtrueを返す タグページ
<?php if(is_tag('mild')): ?>~ <?php endif; ?>
“mild”というスラッグのついたタグのアーカイブページが表示されている場合
<?php if(is_tag(array('sharp','mild','extreme'))): ?>~ <?php endif; ?>
“sharp”または”mild”または”extreme”というスラッグのついたタグのアーカイブページが表示されている場合 注: 配列を引数に使えるのは バージョン 2.5 以降
以下カスタム投稿含めたまとめ
<?php if(is_page()): ?> 固定ページの場合のみ表示 <?php elseif(is_single()): ?> <?php if ( get_post_type() === '投稿タイプ名' ): ?> 指定されたカスタム投稿タイプ名の場合のみ表示 <?php else: ?> 投稿の個別ページの場合のみ表示 <?php endif; ?> <?php elseif(is_category()): ?> カテゴリーページの場合のみ表示 <?php elseif(is_post_type_archive('カスタム投稿タイプ名')): ?> 指定されたカスタム投稿タイプ名のアーカイブページの場合のみ表示 <?php elseif(is_tax('カスタム分類名')): ?> 指定されたカスタム分類名ページの場合のみ表示 <?php elseif(is_tag()): ?> タグのアーカイブページtag.phpの場合のみ表示 <?php elseif(is_search()): ?> 検索結果ページの場合のみ表示 <?php elseif(is_404()): ?> 404ページの場合のみ表示 <?php else: ?> それ以外 <?php endif; ?>