WordPressの特定カテゴリの記事タイトル・日付一覧を取得するコード。
お知らせ一覧などに使えます。
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 | <section> <h2> お知らせ </h2> <?php $arg = array( 'posts_per_page' => 1, // 表示件数 'orderby' => 'date', // ソート種別 'order' => 'DESC', // 最新記事表示 'category_name' => 'markup' // カテゴリーのスラッグ ); $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(); ?> </section> |
あまりないとは思いますが、古い記事から表示したい場合は
'order' => 'DESC',
を「DESC」 → 「ASC」にします。