Hey there, WordPress user! Are you looking for ways to keep your site feeling fresh and engaging for your visitors? One great strategy is to showcase your recent content, especially posts from the previous week that readers may have missed.
In this beginner-friendly guide, I‘ll walk you through exactly how to display last week‘s posts on your WordPress site using a few different methods. Whether you‘re comfortable with code or prefer a plugin solution, you‘ll find the perfect approach for your needs.
Why Display Last Week‘s Posts?
Before we dive into the technical details, let‘s talk about why you might want to highlight your posts from the previous week:
Keep content fresh: Showcasing recent posts helps your site feel current and actively maintained, even if you don‘t publish new content every day.
Boost engagement: Visitors who enjoy your latest content are likely to click through to slightly older posts they may have missed, increasing your pageviews and time on site. In fact, a study by Parse.ly found that promoting recent content can increase pageviews by up to 25%!
Improve navigation: Displaying last week‘s posts provides an additional way for users to explore your site and discover content they‘re interested in.
Enhance SEO: Internal links to your recent posts can help spread link equity and signal to search engines that your content is relevant and interconnected.
Sold on the benefits? Great! Let‘s look at how you can actually implement this on your WordPress site.
Method 1: Custom WP_Query
For WordPress users comfortable with code, writing a custom WP_Query is the most flexible way to display last week‘s posts. You have full control over the query parameters and output.
Here‘s a step-by-step breakdown of how to use WP_Query to show posts from last week:
Open your theme‘s template file where you want to display the last week‘s posts (e.g.,
front-page.php,sidebar.php, etc.).Paste the following code snippet into the desired location:
<?php
$last_week = date(‘Y-m-d‘, strtotime("-1 week"));
$this_week = date(‘Y-m-d‘, strtotime("this week"));
$args = array(
‘post_type‘ => ‘post‘,
‘post_status‘ => ‘publish‘,
‘date_query‘ => array(
array(
‘after‘ => $last_week,
‘before‘ => $this_week,
‘inclusive‘ => true,
),
),
‘posts_per_page‘ => 5,
);
$last_week_query = new WP_Query($args);
if ($last_week_query->have_posts()) :
while ($last_week_query->have_posts()) : $last_week_query->the_post();
?>
<article>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_excerpt(); ?>
</article>
<?php
endwhile;
wp_reset_postdata();
else :
echo ‘<p>No posts from last week found.</p>‘;
endif;
?>Let‘s break down what this code does:
- It calculates the start and end dates for last week using
strtotime()and assigns them to$last_weekand$this_week. - It defines the
$argsarray for theWP_Query, specifying thepost_type,post_status,date_query, andposts_per_page. - It creates a new instance of
WP_Querywith the$argsand assigns it to$last_week_query. - It checks if the query has posts using
have_posts()and enters a loop to output the title and excerpt for each post. - After the loop, it resets the post data using
wp_reset_postdata()to restore the original query.
You can customize this code snippet in a few ways:
- Change the
post_typeto display custom post types in addition to standard posts. - Modify the
posts_per_pageto show more or fewer posts. - Add more parameters to the
$argsarray to filter by category, tag, author, etc. - Customize the output HTML to include featured images, post meta, or other details.
Method 2: functions.php
Another code-based approach is to create a reusable function in your theme‘s functions.php file. This is handy if you want to display last week‘s posts in multiple locations or use a shortcode.
Here‘s how to create a display_last_weeks_posts function:
Open your theme‘s
functions.phpfile.Paste the following code at the end of the file:
function display_last_weeks_posts($num_posts = 5) {
$last_week = date(‘Y-m-d‘, strtotime("-1 week"));
$this_week = date(‘Y-m-d‘, strtotime("this week"));
$args = array(
‘post_type‘ => ‘post‘,
‘post_status‘ => ‘publish‘,
‘date_query‘ => array(
array(
‘after‘ => $last_week,
‘before‘ => $this_week,
‘inclusive‘ => true,
),
),
‘posts_per_page‘ => $num_posts,
);
$last_week_query = new WP_Query($args);
if ($last_week_query->have_posts()) :
echo ‘<ul>‘;
while ($last_week_query->have_posts()) : $last_week_query->the_post();
echo ‘<li><a href="‘ . get_permalink() . ‘">‘ . get_the_title() . ‘</a></li>‘;
endwhile;
echo ‘</ul>‘;
wp_reset_postdata();
else :
echo ‘<p>No posts from last week found.</p>‘;
endif;
}This function works similarly to the WP_Query code snippet, but it:
- Accepts an optional
$num_postsparameter to control the number of posts displayed (default is 5). - Outputs the post titles as a simple unordered list.
To use this function in your template files, just call it like this:
<?php display_last_weeks_posts(3); ?>You can also create a shortcode to use the function in your post or page content:
function last_weeks_posts_shortcode($atts) {
$num_posts = shortcode_atts(array(
‘num_posts‘ => 5,
), $atts)[‘num_posts‘];
ob_start();
display_last_weeks_posts($num_posts);
return ob_get_clean();
}
add_shortcode(‘last_weeks_posts‘, ‘last_weeks_posts_shortcode‘);With the shortcode defined, you can display last week‘s posts in a post or page like this:
[last_weeks_posts num_posts="3"]Method 3: Plugin Power
If you‘re not comfortable working with code, fear not! There are several WordPress plugins that make it easy to display recent and last week‘s posts. One of the most popular and versatile is Recent Posts Widget Extended.
Here‘s how to use the plugin to show posts from last week:
Install and activate the Recent Posts Widget Extended plugin.
Go to Appearance > Widgets in your WordPress dashboard.
Find the Recent Posts Extended widget and drag it to your desired sidebar or widget area.
In the widget settings, customize the options as needed:
- Set the Time Frame to "Last Week."
- Choose the number of posts to display in the Number of Posts field.
- Select which post details to include, such as date, excerpt, or featured image.
- Optionally, filter the posts by category or tag.
- Click the Save button to update the widget settings.
That‘s it! The plugin will now display your posts from last week in the selected widget area.
Tips for Displaying Last Week‘s Posts Effectively
Now that you know three methods for showing last week‘s posts on your WordPress site, here are some tips to make the most of this feature:
1. Choose the Right Location
Think strategically about where to display your last week‘s posts. Some effective locations include:
- Sidebar: A small list of recent posts in the sidebar can catch readers‘ attention as they browse your content.
- Footer: The footer is a great place to display a larger grid or list of recent posts, as visitors often look there for additional navigation options.
- Homepage: Integrating last week‘s posts into your homepage design can help showcase your latest content to new visitors.
2. Optimize the Display Format
Experiment with different ways of formatting your last week‘s posts to find the most engaging and visually appealing layout. Some elements to consider:
- Post title: Display the full title or use a truncated version for a cleaner look.
- Excerpt: Include a short excerpt to give readers a taste of the post content. Adjust the excerpt length to fit your design.
- Featured image: Adding a thumbnail image can make the post listing more eye-catching and clickable.
- Metadata: Include post date, author, category, or other relevant details to provide context.
3. Limit the Number of Posts
Be mindful of how many posts you show in your last week‘s posts display. Too many can overwhelm visitors and clutter your layout. Aim for a balanced number, such as 5-10 posts, depending on your design.
Here‘s a helpful table with recommended post limits for different display locations:
| Location | Recommended Number of Posts |
|---|---|
| Sidebar | 3-5 |
| Footer | 5-10 |
| Homepage | 4-8 |
4. Use Engaging Post Titles
Your post titles play a big role in encouraging clicks from your last week‘s posts display. Follow these best practices for crafting irresistible headlines:
- Keep it concise: Aim for 6-10 words or around 60 characters.
- Use strong, active language that conveys emotion or urgency.
- Include numbers, questions, or curiosity-inducing phrases.
- Hint at the post‘s key benefit or takeaway for the reader.
By optimizing your post titles, you can boost engagement with your recent content and keep visitors on your site longer.
5. Style to Match Your Theme
When integrating last week‘s posts into your site, make sure the styling is cohesive with your overall design. Customize the colors, fonts, and spacing to create a seamless look. You can add custom CSS classes to the code snippets or use the styling options in the Recent Posts Extended plugin.
Advanced Tip: Show Posts from Other Time Periods
While this guide focuses on displaying last week‘s posts, you can easily adapt the code snippets to show posts from other custom time periods. For example, you could modify the date_query to display posts from:
- Last month
- Last quarter
- A specific year
- A custom date range
To do this, adjust the after and before parameters in the date_query using PHP‘s date functions like strtotime() and date(). Here‘s an example of showing posts from the previous month:
$last_month_start = date(‘Y-m-01‘, strtotime("last month"));
$last_month_end = date(‘Y-m-t‘, strtotime("last month"));
$args = array(
// ...
‘date_query‘ => array(
array(
‘after‘ => $last_month_start,
‘before‘ => $last_month_end,
‘inclusive‘ => true,
),
),
// ...
);Keep Exploring!
Displaying last week‘s posts is just one way to keep your WordPress site feeling fresh and engaging. There are many other strategies you can use to optimize your content and design for maximum impact.
Here are a few suggestions for further reading:
- How to Use Featured Images Effectively in WordPress
- 10 Proven Strategies for Boosting Engagement on Your WordPress Blog
- The Ultimate Guide to Crafting Click-Worthy Post Titles
You can also find more useful code snippets and tutorials on the WordPress Code Reference and WordPress Codex.
Conclusion
Congratulations! You now have the knowledge and tools to display last week‘s posts on your WordPress site like a pro. Whether you choose to use a custom WP_Query, create a reusable function, or harness the power of a plugin, you can easily integrate this feature into your site.
Remember to consider the best location, format, and styling for your last week‘s posts display to maximize engagement and visual appeal. Don‘t be afraid to experiment and find what works best for your unique audience and goals.
If you have any questions or suggestions, feel free to leave a comment below. I‘m always happy to help fellow WordPress enthusiasts optimize their sites!
Now go forth and showcase your amazing recent content to the world. Your visitors will thank you for keeping your site fresh and exciting!
