ページ内目次

その投稿が特定のカテゴリーに属しているかを判定する

現在の投稿が、特定のカテゴリーに属しているかどうかを判定するには、in_category()を使用すれば良いようなのですが、カスタム投稿+カスタムタクソノミー(カスタムカテゴリー)の場合は、in_category()ではダメみたいですね。

その代わりに使用できるのが、has_term()です。

if( has_term( 'カテゴリー名', 'カテゴリーグループ名' ) ) {
    // 現在の投稿が、カテゴリーグループの中のカテゴリーが指定されているか
}

こんな感じの条件分岐ができます。

第三引数に投稿IDを指定することもできるので、ループの外でも使えます(デフォルトは現在の投稿)。

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny

↑上へまいりま〜す