Skip to content

Instantly share code, notes, and snippets.

@billerickson
Last active May 31, 2021 21:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/ce5c10c4921ae004869ccc5735e55e52 to your computer and use it in GitHub Desktop.
Save billerickson/ce5c10c4921ae004869ccc5735e55e52 to your computer and use it in GitHub Desktop.
<?php
/**
* Archive Navigation
*
* @author Bill Erickson
* @link https://www.billerickson.net/custom-pagination-links/
*
*/
function ea_archive_navigation() {
$settings = array(
'count' => 6,
'prev_text' => ea_icon( 'arrow-left' ),
'next_text' => ea_icon( 'arrow-right' )
);
global $wp_query;
$current = max( 1, get_query_var( 'paged' ) );
$total = $wp_query->max_num_pages;
$links = array();
// Offset for next link
if( $current < $total )
$settings['count']--;
// Previous
if( $current > 1 ) {
$settings['count']--;
$links[] = ea_archive_navigation_link( $current - 1, 'prev', $settings['prev_text'] );
// Disabled previous button when on first page
} else {
$settings['count']--;
$links[] = '<a class="page-numbers prev disabled" href="#">' . $settings['prev_text'] . '</a>';
}
// Current
$links[] = ea_archive_navigation_link( $current, 'current' );
// Next Pages
for( $i = 1; $i < $settings['count']; $i++ ) {
$page = $current + $i;
if( $page <= $total ) {
$links[] = ea_archive_navigation_link( $page );
}
}
// Next
if( $current < $total ) {
$links[] = ea_archive_navigation_link( $current + 1, 'next', $settings['next_text'] );
}
echo '<nav class="navigation posts-navigation" role="navigation">';
echo '<h2 class="screen-reader-text">Posts navigation</h2>';
echo '<div class="nav-links">' . join( '', $links ) . '</div>';
echo '</nav>';
}
add_action( 'tha_content_while_after', 'ea_archive_navigation' );
/**
* Archive Navigation Link
*
* @author Bill Erickson
* @link https://www.billerickson.net/custom-pagination-links/
*
* @param int $page
* @param string $class
* @param string $label
* @return string $link
*/
function ea_archive_navigation_link( $page = false, $class = '', $label = '' ) {
if( ! $page )
return;
$classes = array( 'page-numbers' );
if( !empty( $class ) )
$classes[] = $class;
$classes = array_map( 'sanitize_html_class', $classes );
$label = $label ? $label : $page;
$link = esc_url_raw( get_pagenum_link( $page ) );
return '<a class="' . join ( ' ', $classes ) . '" href="' . $link . '">' . $label . '</a>';
}
@Erraoudy
Copy link

Erraoudy commented May 5, 2021

Hi,

Thanks. Nice solution. But I can’t know how to use this function. ?

i want to use it in single post pagination i will appreciate your help

Thank you in advance

@Viktor727
Copy link

Awesome !!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment