クリエイター:コーディング備忘録ブログ

コーディング

wpカテゴリ毎の記事一覧を表示する

WPでカテゴリ一覧表示する

カテゴリ一覧表示をしたい時のPHPコードを記述します。
<?php
$categories = get_categories();
foreach($categories as $category) :
?>
<div><a href=”<?php echo get_category_link( $category->term_id ); ?>”><?php echo $category->cat_name; ?></a></div>
<ul>
<?php
query_posts(‘cat=’.$category->cat_ID);
if (have_posts()) : while (have_posts()) : the_post();
?>
<li><a href=”<?php the_permalink() ?>”><?php the_title(); ?></a></li>
<?php endwhile; endif; ?>
</ul>
<?php endforeach; ?>

「get_categories();」は全カテゴリの情報を取得して「foreach」で出力しています。
「query_posts(‘cat=’.$category->cat_ID);」はループ中でカテゴリのIDをクエリに設定して、そのIDのカテゴリに属する記事を出力します。

コメント

この記事へのコメントはありません。

RELATED

PAGE TOP