ブログの記事が古い場合に「この記事は古いから注意してね〜」みたいなお知らせが表示されるサイトをたまに見かけませんか?
このサイトも賞味期限切れの記事がありそうな気配がムンムンしてきたので、半年以上前の記事は古くなってますよ〜って表示しようと思い、調べてみました。
実装したコード
[php]<?php
function day_diff($date1, $date2)…
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; ?>
graffiti on the web
この投稿へのコメント
コメントはまだありません。