Return to Snippet

Revision: 59354
at September 2, 2012 17:57 by thewickedchemist


Initial Code
<?php



function wp_paginate($pgs = '', $rng = 2) {
    $itm = ($rng * 2) + 1;
    global $pgd;
    if (empty($pgd))
        $pgd = 1;
    if ($pgs == '') {
        global $wp_query;
        $pgs = $wp_query->max_num_pgs;
        if (!$pgs) {
            $pgs = 1;
        }
    }
    if (1 != $pgs) {
    // echo the HTML output
        echo "<div class='wp_paginate'>";
        if ($pgd > 2 && $pgd > $rng + 1 && $itm < $pgs)
            echo "<a href='" . get_pagenum_link(1) . "'>&laquo;</a>";
        if ($pgd > 1 && $itm < $pgs)
            echo "<a href='" . get_pagenum_link($pgd - 1) . "'>&lsaquo;</a>";
        for ($i = 1; $i <= $pgs; $i++) {
            if (1 != $pgs && (!($i >= $pgd + $rng + 1 || $i <= $pgd - $rng - 1) || $pgs <= $itm)) {
                echo ($pgd == $i) ? "<span class='current'>" . $i . "</span>" : "<a href='" . get_pagenum_link($i) . "' class='inactive' >" . $i . "</a>";
            }
        }
        if ($pgd < $pgs && $itm < $pgs)
            echo "<a href='" . get_pagenum_link($pgd + 1) . "'>&rsaquo;</a>";
        if ($pgd < $pgs - 1 && $pgd + $rng - 1 < $pgs && $itm < $pgs)
            echo "<a href='" . get_pagenum_link($pgs) . "'>&raquo;</a>";
        echo "</div>\n";
    }
}
?>

Initial URL
http://www.design-ninja.net

Initial Description
Place the function in your functions.php file of your theme...

Then, simply call like this: wp_paginate('', 3);
The first param is optional, the second dictates how many pages before and after the current page link will show...

Initial Title
Paginate WordPress Posts

Initial Tags
wordpress, theme

Initial Language
PHP