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


ピックアップ記事

未解決 twitter関連のOAuth認証が通らない 

2011年05月27日 WordPress
WP to Twitterなど、twitter関連のOAuth認証が通らない 原因は不明だけど、サーバのcURLやら外部向けのHTTPS通信が許可されてないやら、その辺っぽい。 ちなみに現在使っているのは、お名前.comの共用サーバです。 VPSの方も契約してるんで、そっちに移行した方が早いかも。 http://bono.s201.xrea.com/pib/wpm/?p=32 twitt…
「未解決 twitter関連のOAuth認証が通らない 」をはてなブックマークに追加

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

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

簡単に記事が編集出来る WordPress のプラグイン

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