// 30.Aug.2009

A Window into the Archives (Part 2)

In this article I'm going to present the PHP code I wrote for the Window into the Archives module that currently adorns the sidebar on this website. I described the rationale behind the module in an earlier essay.

As I couldn't be bothered to make a proper widget/plug-in around this module the code is located directly within the sidebar.php file of my WordPress theme. The code looks like this:


if (! is_front_page()) {
if (! is_search()) {
$exclusions = array (x,y,z);
if (is_single()) {
global $wp_query;
$exclusions[] = $wp_query->post->ID;
}
$randomArticle = new WP_Query();
$randomArticle->query(array('orderby' => 'rand', 'cat' => x, 'showposts' => 1, 'post_type' => 'post', 'post__not_in' => $exclusions));
if ( $randomArticle->have_posts() ) : while ( $randomArticle->have_posts() ) : $randomArticle->the_post();
echo '<div id="randomArticleContainer"><h2>// From the Archives</h2><div id="randomArticle">';
if (catch_that_image()) {
echo '<div id="randomArticleImageContainer" style="background: url(/scripts/timthumb.php?src=' . catch_that_image() . '&w=x&h=y&zc=1) no-repeat !important;"><a href="' . get_permalink() . '" id="randomArticleLink"><span id="randomArticleSpacer"><span id="randomArticleTitleContainer">' . get_the_title('', '', false) . '</span></span></a></div>';
} else {
echo '<div id="randomArticleTitleNoImage"><a href="' . get_permalink() . '" title="go to: ' . get_the_title('', '', false) . '">' . get_the_title('', '', false) . '</a></div>';
}
echo '</div></div>';
endwhile;
endif;
wp_reset_query();
}
}

Now most of this will be pretty standard stuff for those of you who've created or customised a WordPress theme. But we'll walk through the interesting bits anyway.

First things first, we're not going to display our widget on the front page or on the search results page. There's nowhere to put it on the front page and, for some reason, it breaks whenever I try it on the search results page (I might fix that one day but first I have to figure out what's happening — it's reporting a database error!)


if (! is_front_page()) {
if (! is_search()) {

Then we build up an array of pages to be excluded from the widget. The array is simply a comma-separated list of page ID's that are hard-coded in the module.


$exclusions = array (x,y,z);

Then, if we are viewing an individual post or page (as opposed to an archive list), we capture the ID of that post (or page) and push it onto the $exclusions array. This is how we prevent the current page from being repeated in the archive teaser.


if (is_single()) {
global $wp_query;
$exclusions[] = $wp_query->post->ID;
}

The call to the database comes next. We request a single, random post from a specific category (in my case, the Weblog — replace x with the category ID you want to extract from) and tell the database to ignore the ID's in the $exclusions array. Something is not quite right with this database call as it stands. Whilst it does pull posts from the Weblog category and thus ignores the Asides category, as it should, it also pulls pages — despite the 'post_type' => 'post' parameter (so I had to add all my page ID's to the $exclusions array to prevent this). Can anyone explain why this is the case?


$randomArticle = new WP_Query();
$randomArticle->query(array('orderby' => 'rand', 'cat' => x, 'showposts' => 1, 'post_type' => 'post', 'post__not_in' => $exclusions));

The rest of the code is responsible for generating the output and is pretty self-explanatory. The thumbnail generation is handled in the same manner as those on the front page of this site and is documented in my article, WordPress Thumbnail Sorcery, which you should refer to for an explanation of how these thumbnails are created.

Still to Do:

  • Fix the bug that prevents the module being used on the search results page.
  • Work out why the database query pulls pages as well as posts.
  • Write a wrapper around this code to widgetise it.

Last Revision: October 24th, 2009 at 11:34

Comments are closed.