Home » 2008 » August » 15 » Paging Hemingway Part Deux

Paging Hemingway Part Deux

A little over a year ago, I wrote a plugin that let you page the HemingwayEx home-page. Unfortunately it doesn’t work out-of-the-box with HemingwayEx 1.1 (it also wouldn’t work with vanilla Hemingway). The original version made a few assumptions that are no longer true. So here’s a new version that still works with older versions of HemingwayEx (that had a built-in asides feature) as well as working with the latest version (1.1) of HemingwayEx (where the asides feature has been removed). It will also work with the original Hemingway theme.

The best hook to use is still the query_string filter. Unfortunately, this specific filter is technically deprecated (and may be removed at some point). You’re supposed to use the request filter now. However, the request filter doesn’t seem to provide quite as much functionality as the query_string filter. Namely, there doesn’t appear to be a good way to check for is_home (which is important for our purposes).

<?php

/*
 * Plugin Name: Custom Hemingway Home Query
 * Plugin URI: http://moggy.laceous.com/2008/08/15/paging-hemingway-part-deux/
 * Description: Gives you the ability to use the paging links on your homepage (works with Hemingway and HemingwayEx)
 * Version: 0.2a
 * Author: moggy
 * Author URI: http://moggy.laceous.com
 */

add_filter('query_string', 'custom_hem_home_query');
function custom_hem_home_query($query_string) {
	global $wp_query;
	$wp_query->parse_query($query_string); //required in order to check is_home
	if ($wp_query->is_home) {
		if (strlen($query_string) > 0) {
			$query_string .= '&';
		}
		$query_string .= 'posts_per_page=2';
		global $hemingwayEx;
		if (isset($hemingwayEx)) {
			if (method_exists($hemingwayEx, 'get_asides_category_id')) {
				$category_id = $hemingwayEx->get_asides_category_id();
				if (!is_null($category_id) && !empty($category_id) && is_numeric($category_id)) {
					$query_string .= '&cat=-' . $category_id;
				}
			}
		}
	}
	return $query_string;
}

?>

An alternate way to do this would be to hook into the pre_get_posts action. Unfortunately, there’s a down-side here as well. Namely, if you create a new WP_Query object then this method might interfere with your new WP_Query instance (rather than just affecting the main loop). I ran into this very problem with my asides widget, and had to throw in a category check in the code below.

<?php

/*
 * Plugin Name: Custom Hemingway Home Query
 * Plugin URI: http://moggy.laceous.com/2008/08/15/paging-hemingway-part-deux/
 * Description: Gives you the ability to use the paging links on your homepage (works with Hemingway and HemingwayEx)
 * Version: 0.2b
 * Author: moggy
 * Author URI: http://moggy.laceous.com
 */

add_action('pre_get_posts', 'custom_hem_home_query', 1, 1);
function custom_hem_home_query($wp_query) {
	if (is_home()) { //at this point is_home is already set
		$wp_query->query_vars['posts_per_page'] = 2;
		//$wp_query->query['posts_per_page'] = 2; //only setting query_vars seems to matter
		if (isset($GLOBALS['hemingwayEx'])) {
			if (method_exists($GLOBALS['hemingwayEx'], 'get_asides_category_id')) {
				$category_id = $GLOBALS['hemingwayEx']->get_asides_category_id();
				if (!is_null($category_id) && !empty($category_id) && is_numeric($category_id)) {
					//check if the category has already been set
					//if I don't check this then it breaks my asides widget
					if (strlen($wp_query->query_vars['cat']) <= 0) {
						$wp_query->query_vars['cat'] = '-' . $category_id;
					}
				}
			}
		}
	}
}

?>

To get this to work you’ll still need to follow the 3 steps from my original post. Namely (that’s nĂºmero tres for those of you counting along at home *wink wink*), copying the PHP code into a file (i.e. custom_hem_home_query.php) and uploading to your plugins folder (don’t forget to activate the plugin when you’re ready), commenting out the custom query in your theme’s index.php file (the query_posts line towards the top), and adding the paging links to your home page (also in your theme’s index.php file).


About this entry