トップページにカテゴリごとの記事を表示する
<?php $categories = array(2,3); for ($i=0; $i<count($categories); $i++) : ?> <h2><?php echo esc_html(get_catname($categories[$i])); ?></h2> <?php query_posts('cat='.$categories[$i]); ?> <?php if (have_posts()) : while (have_posts()) : the_post(); // 繰り返し処理開始 ?> <?php endif; endwhile; ?> <?php else: ?> まだ<?php echo esc_html(get_catname($categories[i]))."はありません。" ?> <?php endif; ?>
$categories に 表示させたいカテゴリのIDを入れる
for で カテゴリー分処理を繰り返す。
query_posts で特定のカテゴリを表示する
<?php query_posts('cat=3&showposts=10'); ?>
特定のカテゴリを除外する
<?php query_posts('cat=-5'); ?>
カテゴリIDをマイナスで表示する
カスタム投稿タイプの記事を表示する
<?php query_posts('post_type=info&showposts=5'); ?>
カスタム投稿タイプinfo の記事を5件表示する
複数のカスタム投稿タイプの記事を表示する
<?php $postlist = array( 'post_type' => array('post','info','topics'), 'showposts' => 10, ); ?> <?php query_posts($postlist); ?>
カスタム投稿タイプ post , info , topics の記事10件を表示する