This PHP code snippet is useful for WordPress site owners that want to display previous posts counting backards from the current post ID. In this example I have set the previous post count to 10. If you wanted to show more or less, update the number in here "<= 10; $i++):".
This code should be deployed on single.php or the content-part that's embedded within the single.php. The single.php file is the primary template file for WordPress posts.
Let me familiarise you with how the WordPress PHP snippet works.
1, We're doing a lookup against the /posts db table to find the current post ID. This is the existing that you or a visitor is currently viewing.
2, Once we have identified the current post ID. The next part is to perform a PHP loop to identify the previous 10 posts from the current post ID.
3, The final part is to echo the WordPress post title and the post permalink wrapped in a HTML unordered list element.
The reason I have pulled this Wordpress code snippet for displaying the previous 10 posts is that I could not find a simple way to achieve the above on Stackoverflow and other popular WordPress coding websites. I had to (mostly) build this from scratch. All I could find on those websites was how to display the previous 10 posts from the last most recent post in the /posts db and/or the last post in the category.
Why did I pull this together?
In WordPress, by default viewing a post, the sidebar often displays the last 5-10 posts in the category. Not the previous posts. I wanted to a seek out better programmatic method internal linking opportunites for SEO without the use a plugin.
Thanks for reading. Get in touch if you have any questions.
This code should be deployed on single.php or the content-part that's embedded within the single.php. The single.php file is the primary template file for WordPress posts.
Let me familiarise you with how the WordPress PHP snippet works.
1, We're doing a lookup against the /posts db table to find the current post ID. This is the existing that you or a visitor is currently viewing.
2, Once we have identified the current post ID. The next part is to perform a PHP loop to identify the previous 10 posts from the current post ID.
3, The final part is to echo the WordPress post title and the post permalink wrapped in a HTML unordered list element.
Code:
<ul class="list-unstyled">
<?php
// 1, fetch the current post ID
global $post;
$current_post = $post;
// 2, previous 10 post from the current post ID
for($i = 1; $i <= 10; $i++):
$post = get_next_post();
setup_postdata($post);
// 3, echo post title & post link
echo '<li><a href="' . get_the_permalink() . '">' . get_the_title() . '</a>';
endfor;
$post = $current_post;
?>
</ul>
The reason I have pulled this Wordpress code snippet for displaying the previous 10 posts is that I could not find a simple way to achieve the above on Stackoverflow and other popular WordPress coding websites. I had to (mostly) build this from scratch. All I could find on those websites was how to display the previous 10 posts from the last most recent post in the /posts db and/or the last post in the category.
Why did I pull this together?
In WordPress, by default viewing a post, the sidebar often displays the last 5-10 posts in the category. Not the previous posts. I wanted to a seek out better programmatic method internal linking opportunites for SEO without the use a plugin.
Thanks for reading. Get in touch if you have any questions.
Last edited: