wordpressでカテゴリごと記事一覧取得など
wordpressでカテゴリごとに記事を表示するとか、特定のカテゴリの記事一覧を表示するというのはよくあるので、下記サイトを参考に備忘録。
WordPressでカテゴリー別記事一覧を表示する方法
[解決済み] [閉] 特定のカテゴリーの記事一覧を表示したい。
特定のカテゴリの記事一覧
<?php $posts = get_posts('numberposts=0&category=3'); global $post; ?> <?php if($posts):foreach($posts as $post):setup_postdata($post); ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php endforeach; endif; ?>
カテゴリーある分だけ記事一覧
<?php $categories = get_categories(); foreach($categories as $category) : ?> <h2><a href="<?php echo get_category_link( $category->term_id ); ?>"><?php echo $category->cat_name; ?></a></h2> <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; ?>
カテゴリーIDと順番を指定して記事一覧
<?php $categories = array(3,4,8); for ($i=0; $i<$count($categories); $i++) : ?> <h2><?php echo esc_html(get_catname($categories[$i])); ?></h2> <ul> <?php query_posts('showposts=5&cat='.$categories[$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; ?>