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).

Leave a Reply

9 Comments

  • Rupak Ganguly (Aug 28, 2008)

    Hi, I came across the plugin you wrote and used it on another
    variation of Hemingway by NinjaMonkeys. What I found is that if the
    category under which a post is filed is same across posts, then it
    will page properly, otherwise it does not seem to find the posts. I
    don’t think it is caused by your category check block as that code
    only executes if I have your asides plugin, which I don’t. So that
    leaves only one line of code:
    $wp_query->query_vars['posts_per_page'] = 2; Let me know if have
    come across this issue. I would love to show you but I have this in
    an internal company blog. Let me know when you have some time.
    Thanks, Rupak Ganguly

    Reply

  • moggy (Aug 30, 2008)

    Is this the version of Hemingway you’re using?

    What specific version of WP are you using?

    Any other plugins that might be interfering?

    Have you tried v0.2a (above)… if so, did that work?

    Reply

  • Sam (Dec 25, 2008)

    A pagination issue, possibly the same as above, has popped up on
    the theme authors forum, and I’m having pagination issues as well.
    I’m running HemEx 1.5 on WP 2.7 … clicking “next” resuts in a 404
    error. I wonder if you have any ideas? Thanks, Sam

    Reply

  • moggy (Dec 25, 2008)

    Hey Sam,

    It looks like HemingwayEx 1.5 has its own built-in pagination options. If you’re trying to use this plugin with HemEx 1.5 then you might run into unforeseen issues.

    However, after a quick test with WP2.7 and HemEx1.5 (and no plugins activated), It does appear to produce a 404 response after paging through a couple of pages (when there’s more posts that should be displayed for the next page). I took a look at the code, and it looks like the problem lies with the way the paging was implemented. Basically HemEx1.5 is using a modified query in the theme’s index.php file. I seem to remember exploring this option myself, and found similar results… which is why I ultimately decided to hook into WP actions/filters via a plugin.

    Reply

  • Sam (Dec 26, 2008)

    Thanks moggy, I’m not too code-savvy, but do you suppose I could
    disable that query and try your plugin…what do you think?
    Appreciate your time…. Thanks, Sam

    Reply

  • Rupak Ganguly (Feb 04, 2009)

    @moggy I totally forgot that I had left a comment here but I just
    happened to come back here and saw that you actually replied. Sorry
    to be a tad late ;) but I really do appreciate it. So, yes I am
    using the Ninja Monkeys theme that you linked. I am using WordPress
    MU 1.3.3 (based on WordPress 2.3). Lokoing at the date 08/15 in
    your comment I would say that I am using the version of have here,
    unless you updated the version and did not update the date in the
    comment. Please let me know.

    Reply

  • Jude Pereira (May 10, 2009)

    Even I too have the same paginating problem. It shows a 404 error
    if I try to view posts further than page 2. I think Rupak Ganguly
    might solve the problem… For a temporary solution, I’ll try to
    make a hidden category which is common to all.

    Reply

  • Bill (Jun 26, 2009)

    Using WordPress 2.8 and HemingwayEx 1.5 the process is a little
    simpler now. The three steps Moggy suggests can be modified. (1)
    Copy the PHP code into a file then upload it to the Plugins folder,
    as Moggy describes here. (2) It is no longer necessary to comment
    out the default custom query in the index.php file. If you simply
    uncheck the built-in HemingwayEx paging option in the theme options
    panel, this code will not mess anything up. On the other hand, if
    you do comment out these lines, the widget for Recent Posts will
    not function properly on pages where Moggy’s pagination is present.
    (3) In the index.php file, paste in the code to call for Moggy’s
    pagination. You do not need to erase the built-in HemngwayEx code a
    the bottom of the index.php file. Just put in your code above or
    below it, like this: <?php if
    (function_exists(‘custom_hem_home_query’)): ?> <?php
    next_posts_link(‘ Previous entries’) ?> <?php
    previous_posts_link(‘ | Next entries ‘) ?> <?php endif; ?>
    // The following is the HemingwayEx default code… <?php if
    ($hemingwayEx_options['paging_enabled'] == 1) : ?> <?php
    previous_posts_link(‘ ‘) ?> <?php echo ” ?> That’s it!
    Moggy, thank you for posting all this great info.

    Reply

Archives