Skip to content

Instantly share code, notes, and snippets.

@juanbzpy
Created January 6, 2013 03:04
Show Gist options
  • Save juanbzpy/4464956 to your computer and use it in GitHub Desktop.
Save juanbzpy/4464956 to your computer and use it in GitHub Desktop.

To make sure that this is working on archive, index and search results, use this template for searchform.php

<form role="search" method="get" id="searchbar" action="<?php echo home_url( '/' ); ?>">
<div><label class="screen-reader-text" for="s">Search for:</label>
    <input type="text" value="" name="s" id="s" />
    <input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
// this goes on functions.php
<?php
function pagination( $type = 'plain', $endsize = 1, $midsize = 1 ) {
global $wp_query, $wp_rewrite;
$current = get_query_var( 'paged' ) > 1 ? get_query_var('paged') : 1;
// Sanitize input argument values
if ( ! in_array( $type, array( 'plain', 'list', 'array' ) ) ) $type = 'plain';
$endsize = absint( $endsize );
$midsize = absint( $midsize );
// Setup argument array for paginate_links()
$pagination = array(
'base' => @add_query_arg( 'paged', '%#%' ),
'format' => '',
'total' => $wp_query->max_num_pages,
'current' => $current,
'show_all' => false,
'end_size' => $endsize,
'mid_size' => $midsize,
'type' => $type,
'prev_text' => '&lt;&lt;',
'next_text' => '&gt;&gt;'
);
if ( $wp_rewrite->using_permalinks() )
$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ).'page/%#%/', 'paged' );
if ( ! empty( $wp_query->query_vars['s'] ) )
$pagination['add_args'] = array( 's' => get_query_var( 's' ) );
return paginate_links( $pagination );
}
?>
// and this on index.php or whatever template
<?php echo pagination(); ?>
<?php
global $wp_query;
$total_pages = $wp_query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo '<div class="page_nav">';
echo paginate_links(array(
'base' => @add_query_arg( 'paged', '%#%' ),
'format' => '',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => 'Prev',
'next_text' => 'Next'
));
echo '</div>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment