新着記事一覧を表示@WordPress
投稿(single.php)に「新着記事一覧」を表示する例。
・時系列で降順(新しいものから古いものへ表示)
・表示する件数を指定
・表示している記事は対象から除外
で実装。
*サムネイル画像はアイキャッチ画像を利用。
single.phpの表示させたい場所に以下を記述。
<h2 class="title_btm_content">新着書評</h2> <div id="new_books"> <?php $post_id = get_the_ID(); // 表示している記事のIDを取得して$post_IDに代入 query_posts( array( 'post_type' => 'post', // 全ての投稿を指定 'orderby' => 'date', // 並べ替えを投稿日時で指定 'order' => 'DESC', // 並べ替えには降順を指定(新しいものから表示) 'showposts' => 3, // 表示する件数を指定 'post__not_in' => array($post_id) // 表示している記事IDを指定して除外対象に ) ); if (have_posts()) : while (have_posts()) : the_post(); ?> <dl> <dt><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(100, 200)); ?></a></dt> <dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd> </dl> <?php endwhile; endif; ?> <!-- // #new_books --></div> <?php wp_reset_query(); ?>
- 作者: 藤沢周平
- 出版社/メーカー: 文藝春秋
- 発売日: 1992/09
- メディア: 文庫
- 購入: 1人 クリック: 14回
- この商品を含むブログ (31件) を見る