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


ピックアップ記事

WordPressのREST APIを使って最新記事を取得・表示させる

2017年06月14日 WordPressphp
1.WP REST APIプラグインをインストール 管理画面から「WordPress REST API」プラグインをインストールし、有効化します。 これで http://◯◯◯.com/wp-json/wp/v2/posts にアクセスすれば、記事の情報がjson形式で表示されます。 WordPress REST API jsonを取得・表示させる [php]&l…
「WordPressのREST APIを使って最新記事を取得・表示させる」をはてなブックマークに追加

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

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

wordpress カスタムフィールドの値で記事一覧取得、並び替え

2020年01月10日 WordPress
カスタムフィールドの条件が一つの場合 [php]<?php $args = array( 'posts_per_page' => -1, // 全件取得 'category' => array( 12,14 ), // カテゴリ 'meta_key' => 'finished', //カスタムフィールドのキー 'meta_value'…
「wordpress カスタムフィールドの値で記事一覧取得、並び替え」をはてなブックマークに追加

ページIDで表示する画像を切り替える

2011年11月21日 WordPress
[php] <?php if(is_page(16)){ ?> <img src="<?php echo get_template_directory_uri(); ?>/images/mainImg_<?php the_ID() ;?>.jpg" /> <?php } ?> [/php]
「ページIDで表示する画像を切り替える」をはてなブックマークに追加

WordPressの検索にカテゴリでの絞り込み機能を追加

2011年11月24日 WordPress
[php] <form method="get" id="searchform" action="<?php bloginfo('url'); ?>"> <input type="text" value="<?php the_search_query(); ?>&qu…
「WordPressの検索にカテゴリでの絞り込み機能を追加」をはてなブックマークに追加
© graffiti on the web . All rights reserved. WordPress Theme by comfy