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月30日 WordPress
$categorysの配列を複数指定することで、複数のカテゴリから記事の一覧を取得することが可能です。 [php] <?php $categorys = array(27); for ($i=0; $i<count($categorys); $i++) : ?> <?php query_posts('cat='.$categorys[$i]); if (have_post…
「指定カテゴリに属する記事の一覧を表示」をはてなブックマークに追加

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

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

カテゴリIDからカテゴリ名またはスラッグを取得する

2012年01月24日 WordPress
<?php //カテゴリIDからカテゴリ情報取得 $category = get_category('123'); //カテゴリ名表示 echo $category->cat_name; //スラッグ名表示 echo $category->slug; ?> …
「カテゴリ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