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 のプラグイン

2011年05月26日 WordPress
ということで、タイトル通りのプラグインを紹介します。 Front-end Editor はログインして管理画面から操作しなくても、WPのサイト上から記事やウィジェットなどの編集を可能にしてくれる便利なプラグインです。 Front-end Editorの使用方法 プラグインをダウンロード 管理画面からプラグインを追加するか、このへんからダウンロードして、FTPでサーバにアップしてください。 …
「簡単に記事が編集出来る WordPress のプラグイン」をはてなブックマークに追加

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

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

WordPressのテーマをウィジェットに対応させる

2013年10月06日 web関連WordPressphp
WordPressのテーマをウィジェットに対応させる
Wordpressで自作のテーマを作った場合などは、自分でウィジェットを利用可能にしない限り、ウィジェットは利用出来ません。このサイトのテーマは自作なのですが、今までウィジェットは使う機会がなかったので、未対応のままになっていました(:_;) 今回は、サイドにWordPress Popular Postsプラグインの人気記事一覧を表示させたかったので、調べてみましたよっ!ってことで、ウィジェ…
「WordPressのテーマをウィジェットに対応させる」をはてなブックマークに追加

Facebook と WordPressの連携テスト

2012年01月24日 WordPress
Wordbookerというプラグインを使ってテストしています。
「Facebook と WordPressの連携テスト」をはてなブックマークに追加

カテゴリ名を表示する

2011年11月28日 WordPress
[php] <?php single_cat_title(); ?> [/php]
「カテゴリ名を表示する」をはてなブックマークに追加
© graffiti on the web . All rights reserved. WordPress Theme by comfy