2022年11月12日 WordPress

functions.phpに以下を記述

if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
    function post_is_in_descendant_category( $cats, $_post = null ) {
        foreach ( (array) $cats as $cat ) {
            $descendants = get_term_children( (int) $cat, 'category' );
            if ( $descendants && in_category( $descendants, $_post ) )
                return true;
        }
        return false;
    }
}

条件分岐をしたい場所に以下を記述

<?php if ( in_category(親カテゴリID) || post_is_in_descendant_category(親カテゴリID) ): ?>
    // 「親カテゴリ」もしくは「親カテゴリ」に属する子カテゴリーのものであった場合の出力内容
<?php else: ?>
    //  それ以外の出力内容
<?php endif; ?>

この投稿へのコメント

コメントはまだありません。

コメントを残す

メールアドレスが公開されることはありません。

次のHTML タグと属性が使えます。
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CAPTCHA


ピックアップ記事

表示中ページの条件分岐

2011年11月21日 WordPress
条件分岐一覧 is_front_page()トップページかどうか。 is_home()投稿ページ(固定ページを設定していなければトップページ)かどうか。 is_single()個別記事かどうか。カッコにID、タイトル、スラッグを入れて判定できる。 is_page()ページかどうか。カッコにID、タイトル、スラッグを入れて判定できる。 is_singular()個別記事でもページ…
「表示中ページの条件分岐」をはてなブックマークに追加

WordPress のURLの末尾のスラッシュ(トレイリングスラッシュ)を追加、または削除する

2024年03月03日 web関連WordPress
https://runebook.dev/ja/docs/wordpress/functions/untrailingslashit
「WordPress のURLの末尾のスラッシュ(トレイリングスラッシュ)を追加、または削除する」をはてなブックマークに追加

WordPressで、現在の固定ページが子ページかどうか判別する

2013年10月03日 WordPressphp
WordPressで、現在の固定ページが子ページかどうか判別する
WordPressには、is_front_page() 、is_single() など、様々な条件分岐タグが用意されていますが、子ページかどうかを判別するタグはデフォルトでは用意されていないようなので、調べてみたら、WordPress Codex内にいくつかのパターンが説明されていたので、まとめてみます。 パターン1 判別したい箇所に以下を記述 [php]<?php glob…
「WordPressで、現在の固定ページが子ページかどうか判別する」をはてなブックマークに追加

WordPressサイト全体に認証をかける

2011年11月30日 WordPress
プラグインダウンロード 以下のサイト内から「Angsuman’s Authenticated WordPress Plugin」をダウンロードします。 Angsuman’s Authenticated WordPress Plugin プラグイン編集 プラグインの内容が古くなっていて、最新のWordPressの認証に対応していないので、以下のように書き換えます。 ac_authenti…
「WordPressサイト全体に認証をかける」をはてなブックマークに追加

WordPress ウィジェットに個別のid,classを設定する

2014年08月11日 web関連WordPress
WordPressのウィジェットに個別のid,classを設定して、違うデザインを適用したい場合は、functions.php内でサイドウィジェットを有効にする設定項目の「before_widget」の箇所を以下のように変更します。 [php]register_sidebar(array( 'name' => 'side-widget', 'before_wid…
「WordPress ウィジェットに個別のid,classを設定する」をはてなブックマークに追加
© graffiti on the web . All rights reserved. WordPress Theme by comfy