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のWP-PageNaviが動かない場合の対処法

2012年02月04日 WordPress
WP-PageNaviは、WordPressでページングを可能にしてくれるプラグインですが、以下のように「query_posts」を指定していると、正常に動かないようです。 WP-PageNaviが正常に動かない例 [php]<?php if ( have_posts() ) : query_posts('cat=1&showposts=5' ); ?> [/php] 上記…
「WordPressのWP-PageNaviが動かない場合の対処法」をはてなブックマークに追加

advanced custom fieldの値を更新

2020年02月10日 WordPress
[php] update_field( 'フィールド名', '値', $post_id );[/php] 参考 : https://developers.wano.co.jp/1373/
「advanced custom fieldの値を更新」をはてなブックマークに追加

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

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

WordPressの投稿日からの経過日数を調べる(2つの日付の差分)

2013年10月04日 WordPressphp
WordPressの投稿日からの経過日数を調べる(2つの日付の差分)
ブログの記事が古い場合に「この記事は古いから注意してね〜」みたいなお知らせが表示されるサイトをたまに見かけませんか? このサイトも賞味期限切れの記事がありそうな気配がムンムンしてきたので、半年以上前の記事は古くなってますよ〜って表示しようと思い、調べてみました。 実装したコード [php]<?php function day_diff($date1, $date2)…
「WordPressの投稿日からの経過日数を調べる(2つの日付の差分)」をはてなブックマークに追加

WordPressでトップページ以外の場合にbodyにクラス名を追加する方法

2022年11月12日 WordPress
[php] <?php //トップページ以外の場合にnot_homeクラスを追加 $body_class = ""; if ( !is_front_page() ) { $body_class .= ' not_home'; } ?> <body id="top" <?php body_class( $body_c…
「WordPressでトップページ以外の場合にbodyにクラス名を追加する方法」をはてなブックマークに追加
© graffiti on the web . All rights reserved. WordPress Theme by comfy