An Alternative Way To Unburying Old Posts In WordPress Using PHP

As a blogger, you want your older posts to remain relevant to your new readers by making them visible on your site’s main page. And though there are various ways to achieve this (e.g. plugins, archive pages), Karol K. came up with his own unique and clever method by using PHP.

With his method, you’ll be tinkering a bit with the index.php file. The whole idea is to create some space right after the first post listing and display a random archive post as the second one. The steps involve is as follows:

  • Find the loop.
  • Include “$first_post_marker = true;” just above the loop.
  • Locate the closing “endwhile;” of the loop.
  • Include the following code directly above this “endwhile;”:
if($first_post_marker && strlen($_SERVER['REQUEST_URI']) < 2)    //to make sure that we’re on the first page
{
if(is_home())
{
$week = date(‘W’);
$whatyear = date(‘Y’)-1;
$custom_query = new WP_Query( ‘year=’ . $whatyear . ‘&w=’ . $week . ‘&orderby=rand’);    //random entry from the archive; same week, only one year back
while($custom_query->have_posts())
{
$custom_query->the_post();
/* ==================================
Here’s where you display your post, use the same formatting you’re using in the main loop.
Usually calls like: “the_title();” and “the_content(”);” and so on.
================================== */
break;    //we only need one post
}
wp_reset_postdata();
}
$first_post_marker = false;
 }

Feel free to tell Karol what you think of his method by clicking here.

Incoming search terms for the article:

Related Posts

Printing Messages Backwards With Python

Create a Watercolor and Newspaper Collage in Photoshop

Create A Stunning Red Riding Hood Photo Manipulation

Troubleshooting Performance Problems In Ruby