I would like to be able to display on my homepage a timeline of posts from 7 of the most recent days. For example, if today is January 1, 2013, I would like my blog feed to display something that looks like the following:
-
January 1, 2013
All the posts from this day -
December 31, 2012
All the posts from this day -
December 30, 2012
All the posts from this day -
December 29, 2012
All the posts from this day -
December 28, 2012
All the posts from this day -
December 27, 2012
All the posts from this day -
December 26, 2012
All the posts from this day
Essentially, this feed should always display 7 days worth of posts. In effect, when January 2nd comes, December 26 would be hidden and everything gets bumped by one.
This isn’t outputting working quite how I would like it as when a new week comes along, it will only show 1 day worth of posts, so basically the only day that shows 7 days worth of posts is the 7th day of that week:
<section class="news-timeline">
<?php
$day_check = '';
$today = get_the_date();
$year = date('Y');
$week = date('W');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type'=>'post',
'category_name' => 'whats-happening',
'year' => $year,
'w' => $week,
'paged'=>$paged
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);
if (function_exists('wp_pagenavi')) { wp_pagenavi(); }
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
$day = get_the_date('j');
if ($day == $today) {
echo "<h3>Today</h3>";
$counter = 0;
} elseif ($day != $day_check) {
echo "<h3>" . get_the_date() . "</h3>";
$counter = 0;
}
if ($counter % 3 == 0) {
$apply_css_margin = TRUE;
}
elseif ($counter % 3 == 1) {
$apply_css_margin = TRUE;
} else {
$apply_css_margin = FALSE;
}
$counter++;
?>
<article id="post-<?php the_ID(); ?>" <?php if($apply_css_margin) { post_class('news-timeline-margin'); } else { post_class(); } ?>>
<a href="<?php the_permalink(); ?>">
<div class="meta">
<p class="author">By <?php the_author(); ?></p>
<p class="title"><?php the_title(); ?></p>
</div>
<?php the_post_thumbnail('homepage-features-news'); ?>
</a>
</article>
<?php $day_check = $day; endwhile; endif; ?>
<!-- Older/Newer Pagination -->
<?php if ($wp_query->max_num_pages > 1) : ?>
<div class="pagination">
<?php next_posts_link('<span class="older ss-icon">←</span>previous'); ?>
<?php previous_posts_link('Newer <span class="newer ss-icon">next</span>'); ?>
</div>
<?php endif; ?>
<!-- /Older/Newer Pagination -->
<?php
if (function_exists('wp_pagenavi')){wp_pagenavi();}
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>
<!-- /Featured Loop -->
</section>
You can do all of this outside the template, via the
pre_get_posts
action and a filter onposts_where
.The first function checks if it’s the main query and the home page, if so it applies a filter to the query’s
posts_where
clause.The
posts_where
function limits post selection to today’s date minus 7 days, then immediately removes the filter so it isn’t applied to any other queries on the page.You can put this in your theme’s
functions.php
file, then edit your template to remove all of the query trickery, and just run the normal loop.