WordPressでよく使用する条件分岐を分類してまとめました。
※網羅したものではありませんので全てのタグは公式のリファレンスをご覧ください。

目次

ページ判定系

is_front_page・is_home – ホームページ(トップページ・フロントページ)か?

<?php if ( is_front_page() || is_home() ): ?>
  トップページの処理
<?php else: ?>
  それ以外の処理
<?php endif; ?>

設定 > 表示設定 > ホームページの表示 を設定している場合の分岐

<?php if ( is_front_page() && is_home() ): ?>
    "ホームページの表示→最新の投稿"の場合
<?php else: ?>
    "ホームページの表示→固定ページ"の場合
    <?php if ( is_front_page() ): ?>
      ホームページ(トップページ・フロントページ)の処理
    <?php elseif ( is_home() ): ?>
      投稿ページの処理
    <?php endif; ?>
<?php endif; ?>

is_page – 固定ページか?

<?php if ( is_page() ): ?>
  固定ページの処理
<?php else: ?>
  それ以外の処理
<?php endif; ?>

補足:指定のバリエーション

is_page を例に、さらに条件を指定するバリエーションを紹介します。
その他の条件分岐を使用する場合も基本的には利用可能です。

ID指定
<?php if ( is_page('5') ): ?>
  IDが「5」の固定ページの処理
<?php endif; ?>
スラッグ指定
<?php if ( is_page('about') ): ?>
  スラッグが「about」の固定ページの処理
<?php endif; ?>
タイトル指定
<?php if ( is_page('アバウト') ): ?>
  タイトルが「アバウト」の固定ページの処理
<?php endif; ?>
条件の組み合わせ (array)
<?php if ( is_page(array(5,'about','アバウト')) ): ?>
  IDが「5」or スラッグが「about」or タイトルが「アバウト」の固定ページの処理
<?php endif; ?>
除外 ( !is_page )
<?php if ( !is_page('about') ): ?>
  スラッグが「about」"以外"の固定ページの処理
<?php endif; ?>

is_single – シングルページか?

<?php if ( is_single() ): ?>
  シングルページの処理
<?php else: ?>
  それ以外の処理
<?php endif; ?>
カスタム投稿のシングルページを除外し、デフォルトのシングルページにのみ分岐させる場合
<?php if ( is_single() && !is_singular('XXX') ): ?>
  デフォルトのシングルページの処理
<?php endif; ?>

is_singular(‘post_type’) – カスタム投稿のシングルページか?

<?php if ( is_singular('news') ): ?>
  カスタム投稿タイプが「news」のシングルページの処理
<?php endif; ?>

is_singular – 固定ページ or シングルページ or 添付ファイルページか?

<?php if ( is_singular() ): ?>
  固定ページ or シングルページ or 添付ファイルページ の処理
<?php else: ?>
  それ以外の処理
<?php endif; ?>

補足:is_singular を分解すると?

is_singular() は様々な投稿タイプを判定できる関数。 is_singular( array( ‘post’ , ‘page’ , ‘attachment’ ) ) と考えれば、前述の is_singular(‘post_type’) も理解できる。
注意点として、is_singular()ではIDやスラッグの指定を行うことができない。

【参考】WordPressの関数「is_single()」と「is_singular()」の違いを整理
https://www.bring-flower.com/blog/defference-between-single-and-singular/

is_404 – 404ページか?

<?php if ( is_404() ): ?>
  404ページの処理
<?php else: ?>
  それ以外の処理
<?php endif; ?>

is_page_template – ページテンプレートがxxxか?

<?php if ( is_page_template('page-templates/about.php') ): ?>
  page-templates/about.phpが使用されている場合の処理
<?php else: ?>
  それ以外の処理
<?php endif; ?>

アーカイブ系

is_archive – アーカイブページか?

<?php if ( is_archive() ): ?>
  アーカイブページの処理
<?php else: ?>
  それ以外の処理
<?php endif; ?>

アーカイブの1ページ目か?

<?php if ( is_archive() && !is_paged() ): ?>
  アーカイブの1ページ目の処理
<?php else: ?>
  それ以外の処理
<?php endif; ?>

is_category – カテゴリアーカイブページか?

<?php if ( is_category() ): ?>
  カテゴリアーカイブページの処理
<?php else: ?>
  それ以外の処理
<?php endif; ?>

is_tag – タグアーカイブページか?

<?php if ( is_tag() ): ?>
  タグアーカイブページの処理
<?php else: ?>
  それ以外の処理
<?php endif; ?>

is_tax – カスタムタクソノミーがxxxのアーカイブページか?

<?php if ( is_tax('hoge') ): ?>
  カスタムタクソノミーが「hoge」のアーカイブページの処理
<?php else: ?>
  それ以外の処理
<?php endif; ?>

is_post_type_archive – カスタム投稿タイプがxxxのアーカイブページか?

<?php if ( is_post_type_archive('news') ): ?>
  カスタム投稿タイプが「news」のアーカイブページの処理
<?php else: ?>
  それ以外の処理
<?php endif; ?>

複数指定の場合

<?php if ( is_post_type_archive(array('news', 'event'))): ?>
  カスタム投稿タイプが「news」と「event」のアーカイブページの処理
<?php endif; ?>

<!-- ※ブラケット[ ] を使用して、このような記述も可能です。 -->
<?php if ( is_post_type_archive(['news', 'event']) ): ?>
  カスタム投稿タイプが「news」と「event」のアーカイブページの処理
<?php endif; ?>

is_date – 日付に紐づくアーカイブページか?

<?php if ( is_date() ): ?>
  年別・月別・日別 それぞれのアーカイブページの処理
<?php else: ?>
  それ以外の処理
<?php endif; ?>

年別のみ

<?php if ( is_year() ): ?>
  年別のアーカイブページの処理
<?php endif; ?>

月別のみ

<?php if ( is_month() ): ?>
  月別のアーカイブページの処理
<?php endif; ?>

日別のみ

<?php if ( is_day() ): ?>
  日別のアーカイブページの処理
<?php endif; ?>

is_author – 著者のアーカイブページか?

<?php if ( is_author() ): ?>
  著者のアーカイブページの処理
<?php else: ?>
  それ以外の処理
<?php endif; ?>

機能判定系

has_tag – 特定のタグを持っているか?

<?php if ( has_tag('hoge') ): ?>
  「test」というタグを持っている場合の処理
<?php elseif ( has_tag('fuga') ): ?>
  「fuga」というタグを持っている場合の処理
<?php else: ?>
  それ以外の処理
<?php endif; ?>

is_paged – 2ページ目以降のページ(複数にわたるページ)か?

アーカイブページ等で複数ページに分かれている時を指す。
記事を”nextpage”などで分割している場合は当てはまらない。

<?php if ( is_paged() ): ?>
  2ページ目以降のページの処理
<?php else: ?>
  それ以外の処理
<?php endif; ?>

has_post_thumbnail – アイキャッチ画像があるか?

<?php if ( has_post_thumbnail() ): ?>
    アイキャッチ画像指定されている場合は出力する
    <?php the_post_thumbnail(); ?>
<?php endif; ?>

the_post_thumbnail – サムネイル出力

// パラメータなし -> 初期値:post-thumbnail
the_post_thumbnail();                     
// サムネイル (デフォルト 150px x 150px :最大値)
the_post_thumbnail( 'thumbnail' ); 
// 中サイズ   (デフォルト 300px x 300px :最大値)
the_post_thumbnail( 'medium' );           
// 大サイズ   (デフォルト 640px x 640px :最大値)
the_post_thumbnail( 'large' );
// フルサイズ (アップロードした画像の元サイズ)
the_post_thumbnail( 'full' );             
// 他のサイズ
the_post_thumbnail( array( 100, 100 ) );  

wp_is_mobile – モバイル端末か?

$_SERVER[‘HTTP_USER_AGENT’]の値に基づき、true または false を返します。
スマホの他にタブレットも含むことに注意です。

<?php if ( wp_is_mobile() ): ?>
  モバイル端末(スマホ・タブレット)の処理
<?php else: ?>
  それ以外の処理
<?php endif; ?>

is_user_logged_in – ログインしているか?

<?php if ( is_user_logged_in() ): ?>
  ログインしている場合
<?php else: ?>
  ログインしていない場合
<?php endif; ?>

■参考
・Conditional Tags | Theme Developer Handbook
https://developer.wordpress.org/themes/basics/conditional-tags/
・Code Reference | WordPress Developer Resources
https://developer.wordpress.org/reference/
・条件分岐タグ – WordPress Codex 日本語版
http://wpdocs.osdn.jp/%E6%9D%A1%E4%BB%B6%E5%88%86%E5%B2%90%E3%82%BF%E3%82%B0
・WordPressの関数「is_single()」と「is_singular()」の違いを整理
https://www.bring-flower.com/blog/defference-between-single-and-singular/


コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

Web制作

前の記事

【Shopify】schema type分類とプロパティ