Skip to content

Instantly share code, notes, and snippets.

@daltonrooney
Created September 11, 2014 23:23
Show Gist options
  • Save daltonrooney/8f6f92d8be5d59db51f6 to your computer and use it in GitHub Desktop.
Save daltonrooney/8f6f92d8be5d59db51f6 to your computer and use it in GitHub Desktop.
Numerical pagination for supplemental SearchWP engine
<!-- begin pagination -->
<?php if( $engine->maxNumPages > 1 ) :
echo '<nav class="pagination"><ul>';
$max = intval( $engine->maxNumPages );
/**ID of your custom search results page here**/
$permalink = get_permalink(479);
$prevPage = $swppg > 1 ? $swppg - 1 : false;
$nextPage = $swppg < $engine->maxNumPages ? $swppg + 1 : false;
/** Add current page to the array */
if ( $swppg >= 1 )
$links[] = $swppg;
/** Add the pages around the current page to the array */
if ( $swppg >= 3 ) {
$links[] = $swppg - 1;
$links[] = $swppg - 2;
}
if ( ( $swppg + 2 ) <= $max ) {
$links[] = $swppg + 2;
$links[] = $swppg + 1;
}
/** Previous Post Link */
if ( $prevPage )
printf( '<li><a href="%s?swpquery=%s&swppg=%s">%s</a></li>',$permalink, $query, $prevPage,'&laquo; Prev' );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $swppg ? ' class="active"' : '';
printf( '<li%s><a href="%s?swpquery=%s&swppg=%s">%s</a></li>', $class, $permalink, $query, 1,1 );
if ( ! in_array( 2, $links ) )
echo '<li>&hellip;</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $swppg == $link ? ' class="active"' : '';
printf( '<li%s><a href="%s?swpquery=%s&swppg=%s">%s</a></li>', $class, $permalink, $query, $link, $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $swppg == $max ? ' class="active"' : '';
printf( '<li%s><a href="%s?swpquery=%s&swppg=%s">%s</a></li>', $class, $permalink, $query, $max, $max );
}
/** Next Post Link */
if ( $nextPage )
printf( '<li><a href="%s?swpquery=%s&swppg=%s">Next &raquo;</a></li>',$permalink, $query, $nextPage );
echo "</ul></nav>";
endif;?>
<!--end pagination-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment