Skip to content

Instantly share code, notes, and snippets.

@zzap
Last active September 30, 2016 20:06
Show Gist options
  • Save zzap/103415e0c8015731cd46ab68ad20cb3e to your computer and use it in GitHub Desktop.
Save zzap/103415e0c8015731cd46ab68ad20cb3e to your computer and use it in GitHub Desktop.
Numbered pagination, full and simple, for default and custom archives/queries.
<?php
/**
* Numbered pagination
*
* Rendering full pagination on archive pages in numbered form.
* Also, add 'first' and 'last' links because 'prev' and 'next'
* are just not enough sometimes.
*
* @link http://codex.wordpress.org/Function_Reference/paginate_links
*
* @param string $html_id Id for pagination nav
* @param obj $query WP_Query object, needed for paginating custom queries
* @return string Returns pagination markup
*/
function zzap_content_pagination( $html_id, $query = null ) {
global $wp_query;
// escape id attribute
$html_id = esc_attr( $html_id );
// set prev/next and first/last strings
$prev_text = sprintf( __( '%1$s %2$s', 'zzap' ),
$prev_arrow = '<span class="meta-nav">&lsaquo;</span>',
$prev = '<span class="pag-text prev-text">' . __( 'Prev', 'zzap' ) . '</span>'
);
$next_text = sprintf( __( '%1$s %2$s', 'zzap' ),
$next = '<span class="pag-text next-text">' . __( 'Next', 'zzap' ) . '</span>',
$next_arrow = '<span class="meta-nav">&rsaquo;</span>'
);
$first_text = sprintf( __( '%1$s %2$s', 'zzap' ),
$first_arrow = '<span class="meta-nav">&laquo;</span>',
$first = '<span class="pag-text first-text">' . __( 'First', 'zzap' ) . '</span>'
);
$last_text = sprintf( __( '%1$s %2$s', 'zzap' ),
$last = '<span class="pag-text last-text">' . __( 'Last', 'zzap' ) . '</span>',
$last_arrow = '<span class="meta-nav">&raquo;</span>'
);
// assume it's global query if no custom is set
if ( empty( $query ) ) {
$total = $wp_query->max_num_pages;
} else {
$total = $query->max_num_pages;
}
if ( $total > 1 ) :
// Get the current page
if ( !$current_page = get_query_var( 'paged' ) ) {
$current_page = 1;
}
// Structure of 'format' depends on whether we're using pretty permalinks
$permalinks = get_option( 'permalink_structure' );
// Fix url for search pages
if ( is_search() ) {
$s = get_search_query();
$base = empty( $permalinks ) ? esc_url( home_url( '/' ) ) . '&page=%#%?s=' . $s : esc_url( home_url( '/' ) ) . 'page/%#%/?s=' . $s;
$format = '';
}
else {
$base = get_pagenum_link(1) . '%_%';
$format = empty( $permalinks ) ? '&page=%#%' : 'page/%#%/';
}
// set 'current' string
$currently = sprintf( __( 'Page %1$s of %2$s', 'zzap' ),
'<span class="current-number">' . $current_page . '</span>',
'<span class="total-number">' . $total . '</span>'
);
// let's show all only if there are less than 10 pages
if ( $total > 10 ) {
$show_all = false;
} else {
$show_all = true;
}
// define first and last links
$first_link = get_pagenum_link( 1 );
$last_link = get_pagenum_link( $total );
$pages = paginate_links( array(
'base' => $base,
'format' => $format,
'current' => $current_page,
'total' => $total,
'show_all' => $show_all,
'end_size' => 1, // show last page, if last is the current also show one before
'mid_size' => 1, // show one page before and one page after the current
'prev_next' => true,
'prev_text' => $prev_text,
'next_text' => $next_text,
'type' => 'array'
) ); ?>
<div class="pagination-wrapper">
<nav id="<?php echo $html_id; ?>" class="pagination pagination--number" role="navigation">
<h3 class="assistive-text"><?php _e( 'Post navigation', 'zzap' ); ?></h3>
<ul class="page-numbers">
<?php
// get 'first' page link
if ( $current_page != 1 ) {
echo '<li><a class="first page-numbers" href="' . $first_link . '">' . esc_html( $first_text ) . '</a></li>';
}
// get all pages
foreach ( $pages as $link ) {
echo '<li>' . $link . '</li>';
}
// get 'last' page link
if ( $current_page != $total ) {
echo '<li><a class="last page-numbers" href="' . $last_link . '">' . esc_html( $last_text ) . '</a></li>';
}
?>
</ul><!-- page-numbers -->
</nav><!-- #<?php echo $html_id; ?> .pagination.pagination--number -->
<div class="currently-on">
<?php echo $currently; ?>
</div><!-- currently-on -->
</div><!-- pagination-wrapper -->
<?php endif; // $total > 1
}
/**
* Simple numbered pagination
*
* Rendering pagination on archive pages in numbered form.
* Simple pagination has no 'first' and 'last' item,
* no text 'Page $current of $total' and has only arrows
* for 'next' and 'prev' items.
*
* @link http://codex.wordpress.org/Function_Reference/paginate_links
*
* @param string $html_id Id for pagination nav
* @param obj $query WP_Query object, needed for paginating custom queries
* @return string Render pagination markup
*/
function zzap_simple_content_pagination( $html_id, $query = null ) {
global $wp_query;
// escape id attribute
$html_id = esc_attr( $html_id );
// arrows for prev and next links
$prev = __( '&lsaquo;', 'zzap' );
$next = __( '&rsaquo;', 'zzap' );
// assume it's global query if no custom is set
if ( empty( $query ) ) {
$total = $wp_query->max_num_pages;
} else {
$total = $query->max_num_pages;
}
if ( $total > 1 ) :
// Get the current page
if ( ! $current_page = get_query_var( 'paged' ) ){
$current_page = 1;
}
// Structure of 'format' depends on whether we're using pretty permalinks
$permalinks = get_option( 'permalink_structure' );
// Fix url for search pages
if ( is_search() ) {
$s = get_search_query();
$base = empty( $permalinks ) ? esc_url( home_url( '/' ) ) . '&page=%#%?s=' . $s : esc_url( home_url( '/' ) ) . 'page/%#%/?s=' . $s;
$format = '';
} else {
$base = get_pagenum_link(1) . '%_%';
$format = empty( $permalinks ) ? '&page=%#%' : 'page/%#%/';
}
// let's show all only if there are less than 10 pages
if ( $total > 10 ) {
$show_all = false;
} else {
$show_all = true;
}
$pages = paginate_links( array(
'base' => $base,
'format' => $format,
'current' => $current_page,
'total' => $total,
'show_all' => $show_all,
'end_size' => 1, // show last page, if last is the current also show one before
'mid_size' => 1, // show one page before and one page after the current
'prev_next' => true,
'prev_text' => $prev,
'next_text' => $next,
'type' => 'array'
) ); ?>
<div class="pagination-wrapper">
<nav id="<?php echo $html_id; ?>" class="pagination pagination--number" role="navigation">
<h3 class="assistive-text"><?php _e( 'Post navigation', 'zzap' ); ?></h3>
<ul class="pagination page-numbers">
<?php
foreach ( $pages as $link ) :
echo '<li>' . $link . '</li>';
endforeach; // $pages as $link
?>
</ul><!-- pagination page-numbers -->
</nav><!-- #<?php echo $html_id; ?> .pagination.pagination--number -->
</div><!-- pagination-wrapper -->
<?php endif; // $total > 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment