Ah, the joys of trying to get old content back in front of readers!
With over 1,200 posts in the archives, I’m hunting down ways of laying breadcrumbs down for the older writing. Since ‘related post’ plug-ins are incredibly hit and miss (given the nature of the Multiblog) I’ve written the following code for single post pages.
This creates a list of all of the posts that were made on the same day throughout the archives. It’s a fun random romp through the archives and it will be interesting to see how many people actually click on the links.
The base for this code modification is Mystique WordPress Theme created by Digital Nature now defunct version 2.4.2. Most of the code below should work under any theme, but the specific locations give are from Mystique.
Always make a copy of the file before you start editing so that you can replace it if something goes wrong!
This code is inserted into single.php right after the following line:
< ?php the_content(__('More >', 'mystique')); ?>
The full code is below and after that I’ll walk through it lines-by-line, as always:
< ?php
$GetMonth = get_the_date( "m", $before, $after, FALSE );
$GetDay = get_the_date( "d", $before, $after, FALSE );
$GetID = get_the_ID();
$string = "monthnum=".$GetMonth."&day=".$GetDay;
echo '<pre> </pre>';
echo 'Posts From this Day in Ages Past
';
echo '';
$second_query = new WP_Query( $string);
while( $second_query->have_posts() ) : $second_query->the_post();
if ( get_the_ID() != $GetID ) :
- < ?php the_title(); echo " ("; echo get_the_date( "Y", $before, $after, FALSE ); echo ")"; ?>
< ?php endif;
endwhile;
wp_reset_postdata();
echo '
'; ?>
The first section uses get_the_date to break down the date of the post into a month and day, and get_the_ID to get the post’s ID. Then it combines the month and the day into a search filter we can use to grab only those posts that match.
< ?php
$GetMonth = get_the_date( "m", $before, $after, FALSE );
$GetDay = get_the_date( "d", $before, $after, FALSE );
$GetID = get_the_ID();
$string = "monthnum=".$GetMonth."&day=".$GetDay;
Now that we've got the basic information, it's time to open the BLOCKQUOTE and UL list. Since I want this to show up whether or not there are any matching posts, I put it in before the WHILE loop.
I've also added the PRE set before the BLOCKQUOTE because the images had started overlapping it recently. Not sure why, but I'm working on figuring that out (I'll come back and edit this once I do).
Note: I'm using the HTML code for < below since WordPress insists on killing the code window if I use the actual PRE bookends. Arg.
echo '<pre> </pre>';
echo 'Posts From this Day in Ages Past
';
echo '';
Next we pull a list of posts using WP_Query and the filter string we made above.
$second_query = new WP_Query( $string);
The next line opens up the WHILE loop that checks to see if there are any posts left in the list (have_posts) before moves to the next in line (the_post).
while( $second_query->have_posts() ) : $second_query->the_post();
The following IF uses get_the_ID to check and make sure that the post you are currently reading is excluded from the list.
if ( get_the_ID() != $GetID ) :
This very long line creates the LI entry and adds the year that the post is from in parentheses.
For the URL, it checks to see if there is a value in $title_url, if not it uses the_permalink().
The _e function returns the phrase “Permanent Link:” in the local language for the reader. I’m not sure if this is really needed or not, nothing I find on Google seems to say if anyone/anyone uses it.
Then it uses the_title to get the post title.
Lastly is snags the year the post was made using get_the_date and puts it in parentheses.
The last bit of code closes and clears everything.
The ENDIF closes the IF where we checked the ID of the post. The ENDWHILE closes the WHILE that was moving us through the query. wp_reset_postdata closes and clears out the query we opened using WP_Query.
And of course we have the closing tags for the UL and the BLOCKQUOTE, as well as the closing tag for the PHP code itself.
< ?php endif;
endwhile;
wp_reset_postdata();
echo ''; ?>
And that’s a wrap!
Not sure if this will turn out to be a useful bit of code for anyone else, but I figure it can’t hurt to share. 🙂
Leave a Reply