カテゴリーIDで記事を振り分ける

現在このWeb Diary.netはWordPressを使って構築しています。

トップページでDiaryとReferenceと言うカテゴリーで記事の振り分けを行っています。

このやり方がわからず作成まで時間がかかったので簡単にメモを残しておきます。

Aカテゴリーが、Bカテゴリーの祖先にあたるかどうかを返してくれるcat_is_ancestor_ofという関数があるようです。

<?php
$post_cats = get_the_category();
if ( $post_cats[0]->cat_ID == 1 || $post_cats[0]->category_parent == 1 || cat_is_ancestor_of( 1, (int)$post_cats[0]->category_parent ) ) { ?>
親カテゴリーAの記事の表示内容
<?php } elseif ( $post_cats[0]->cat_ID == 5 || $post_cats[0]->category_parent == 5 || cat_is_ancestor_of( 5, (int)$post_cats[0]->category_parent ) ) { ?>
親カテゴリーBの記事の表示内容
<?php } else { ?>
その他の記事の表示内容
<?php } ?>

参照元URL:http://ja.forums.wordpress.org/topic/1290

トラックバック

コメント