Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I saw this presentation about why pagination on the Internet is weird. https://speakerdeck.com/u/espylaub/p/pagination-on-the-internet-and-why-its-weird It inspired me to try and make a WordPress plugin to do just that. Here's what I've got so far...

<?php
/*
Plugin Name: Reverse Pagination
Description: Pagination starts higher and goes lower for more consistent archive page numbering.
Author: Russell Heimlich
Version: 0.1
*/ 


function reverse_pagination_pre_get_posts($t) {  
     $q = $t->query_vars;
     if ( empty($q['order']) || ((strtoupper($q['order']) != 'DESC') && (strtoupper($q['order']) != 'ASC')) ) {
        $q['order'] = 'ASC';
     }

     $t->query_vars = $q;
}

function reverse_pagination_wp() {
    global $wp_query;
    $wp_query->posts = array_reverse($wp_query->posts);
}

if( !is_admin() ) {
    add_action('pre_get_posts', 'reverse_pagination_pre_get_posts');
    add_action('wp', 'reverse_pagination_wp');
} 

I hook into the pre_get_posts action and switch the order query var to ASC instead of the default DESC.

Next I needed to reverse the order of the posts returned so they are in reverse chronological order. I chose the wp action for the reasons outlined in this answer http://wordpress.stackexchange.com/a/22768

So things are looking good so far. I just need to fix the first page. Right now if my blog has 50 pages and there is no paged parameter set the blog index page will show my very first post instead of the latest post. I need some logic to check if paged isn't set to set it to the same value of $wp_query->max_num_pages for that query.

I also need to disable the canonical redirect that removes /page/1/ from the URL and tweak the get_pagenum_link() function so it returns /page/1/ for get_pagenum_link(1) http://hitchhackerguide.com/2011/02/12/get_pagenum_link/

If there is anything else that can be added to make this plugin better please speak up! Thanks and I hope we can make this happen by working together on it.

share|improve this question

1 Answer 1

This function will give you a url for a page number without stripping the url for page 1:

function get_pagenum_link_full($pagenum = 1, $escape = true ) {
    global $wp_rewrite;
    $pagenum = (int) $pagenum;
    $request = remove_query_arg( 'paged' );
    $home_root = parse_url(home_url());
    $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
    $home_root = preg_quote( $home_root, '|' );
    $request = preg_replace('|^'. $home_root . '|i', '', $request);
    $request = preg_replace('|^/+|', '', $request);
    if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
        $base = trailingslashit( get_bloginfo( 'url' ) );
        $result = add_query_arg( 'paged', $pagenum, $base . $request );
    }
}

Modified from the original code for get_pagenum_link() as defined here http://phpxref.ftwr.co.uk/wordpress/wp-includes/link-template.php.source.html#l1408

get_pagenum_link_full(1); will give you something like http://www.url.com/category/page/1 depending on your permalink settings.

share|improve this answer
    
Cool thanks for the help. –  kingkool68 Feb 9 '13 at 3:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.