前回Custom Post type UIを使ったカスタム投稿タイプを紹介したが、今回はその投稿したポストをトップページなどに一覧で表示するやり方をご紹介します。
1 2 3 4 5 6 |
<?php $loop = new WP_Query( array( 'post_type' => ’news', 'posts_per_page' => 5 ) ); ?> <?php while ( $loop->have_posts() ) : $loop->the_post(); ?> <?php the_time("Y-m-d"); ?> <p id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?> <?php endwhile; ?> |
スポンサードリンク
以下解説
1 |
<?php $loop = new WP_Query( array( 'post_type' => ’news', 'posts_per_page' => 5 ) ); ?> |
ループを作成する。「news」部分はポストタイプを記入する。連想配列で5つ分の記事を取得。
1 |
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?> |
ループを開始する。
1 |
<?php the_time("Y-m-d"); ?> |
日付を取得
1 |
<p id="post-<?php the_ID(); ?>" <?php post_class(); ?>> |
IDとクラスを取得
1 |
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?> |
タイトルを取得してパーマリンクで囲ってあげる。
1 |
<?php endwhile; ?> |
5回繰り返したらループを抜ける。
以上。
……最初の一行以外ただのループを作る方法だね。