wp カテゴリー別記事一覧を表示(サイドバー)

基本

<?php
$categories = get_categories();
foreach($categories as $category) :
?>
<h3><?php echo $category->cat_name; ?></h3>
<ul>
<?php
query_posts('showposts=5&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; ?>

カテゴリーのIDと順番を指定して表示

<?php
$categorys = array(4,6,5);
for ($i=0; $i<count($categorys); $i++) :
?>
<h3><?php echo esc_html(get_catname($categorys[$i])); ?></h3>
<ul>
<?php
query_posts('showposts=5&cat='.$categorys[$i]);
if (have_posts()) : while (have_posts()) : the_post();
?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php else: ?>
<li><?php echo esc_html(get_catname($categorys[$i]))."はまだありません。"; ?></li>
</ul>
<?php endif; ?>
<?php endfor; ?>
PAGE TOP