過去7日間の特定カテゴリ記事一覧の作り方
query_posts でも同じことが出来るが、非推奨なので get_posts を使った。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php $args = array('posts_per_page' => 70, 'category' => 1); // 表示数とカテゴリID $postslist = get_posts($args); foreach ($postslist as $post) : setup_postdata($post); $days = 8; $today = date_i18n('U'); $entry = get_the_time('U'); $progress = ($today - $entry) / 86400; // 1日=86400秒 if ($days > $progress) { ?> ループさせるコード <?php } ?> <?php endforeach; wp_reset_postdata(); ?> |
表示数を限定せず、カテゴリを含む全ての記事を対象にしたい
2行目を、
1 | posts_per_page' => -1 |
ただこの場合、例えば対象カテゴリが3,000記事ある場合は、3,000記事全てをDBアクセスしてしまう。
サーバー負荷がもの凄いことになり、サイト表示が激重になるので要注意。
複数のカテゴリを含めたい
2行目を、
1 | 'category' => array(1, 50, 84) |
日数を変更したい
6行目 $days の数字を変更
コメントを投稿する