特定のカテゴリの投稿の一覧を10件表示させる。(自分用メモ)
wordpressで、特定のカテゴリの投稿の一覧を10件表示させる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php $arg = array( 'posts_per_page' => 10, // 表示する件数 'orderby' => 'date', // 日付でソート 'order' => 'DESC', // DESCで最新から表示、ASCで最古から表示 'category_name' => 'cycle' // 表示したいカテゴリーのスラッグを指定 ); $posts = get_posts( $arg ); if( $posts ): ?> <ul> <?php foreach ( $posts as $post ) : setup_postdata( $post ); ?> <li><?php the_time( 'Y.m.d' ); ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> <?php endif; wp_reset_postdata(); ?> |
ページネーション追加版。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<?php $args = array( 'posts_per_page' => 10, //表示件数 'cat' => 1, //カテゴリID 'paged' => $paged, 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => 'post', 'post_status' => 'publish' ); $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ): while ( $the_query->have_posts() ): $the_query->the_post(); //カテゴリ情報を取得 $cat = get_the_category(); $cat_id = $cat[ 0 ]->cat_ID; ?> <div class="<?php foreach($cat as $c) {echo ' '.$c->category_name;} ?>"> <p><span class="post-date"><?php the_time('Y.m.d'); ?></span> <?php foreach ( $cat as $c ) { //カテゴリ名を表示 echo '<span class="label label-warning ' . $c->category_name . '"><a href="' . $c->category_name . ' ">' . $c->cat_name . '</a></span> '; } ?> </p> <h3><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h3> <p><?php the_content() ?></p> </div> <?php endwhile; ?> <?php else: ?> <p>このカテゴリの記事はみつかりませんでした。</p> <?php endif; ?> <div class="pagenation"> <?php //ページネーション if ( $the_query->max_num_pages > 1 ) { echo paginate_links( array( 'base' => get_pagenum_link( 1 ) . '%_%', 'format' => 'page/%#%/', 'current' => max( 1, $paged ), 'total' => $the_query->max_num_pages, 'type' => 'list', 'prev_text' => '« 前へ', 'next_text' => '次へ »' ) ); } wp_reset_postdata(); ?> </div> |