Skip to content

Instantly share code, notes, and snippets.

@kirandash
Created May 18, 2017 11:21
Show Gist options
  • Save kirandash/721e56e714a34948adce162889e29e46 to your computer and use it in GitHub Desktop.
Save kirandash/721e56e714a34948adce162889e29e46 to your computer and use it in GitHub Desktop.
Custom Pagination
<?php
/**
* Using $paged redirects /page/2 to page 1
*/
add_filter('redirect_canonical','pif_disable_redirect_canonical');
function pif_disable_redirect_canonical($redirect_url) {
if (is_singular()) $redirect_url = false;
return $redirect_url;
}
<?php
if ( ! function_exists( 'thirdrock_paging_nav' ) ) :
/**
* Display navigation to next/previous set of posts when applicable.
*
* @return void
*/
function thirdrock_paging_nav() {
// Don't print empty markup if there's only one page.
if ( $GLOBALS['wp_query']->max_num_pages < 2 ) {
return;
}
$paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
$pagenum_link = html_entity_decode( get_pagenum_link() );
$query_args = array();
$url_parts = explode( '?', $pagenum_link );
if ( isset( $url_parts[1] ) ) {
wp_parse_str( $url_parts[1], $query_args );
}
$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
$pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
$format = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
$format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';
// Set up paginated links.
$links = paginate_links( array(
'base' => $pagenum_link,
'format' => $format,
'total' => $GLOBALS['wp_query']->max_num_pages,
'current' => $paged,
'mid_size' => 2,
'add_args' => array_map( 'urlencode', $query_args ),
'prev_text' => '<i class="fa fa-backward"></i>',
'next_text' => '<i class="fa fa-forward"></i>',
'type' => 'list',
) );
if ( $links ) :
?>
<nav class="navigation paging-navigation" role="navigation">
<h1 class="screen-reader-text"><?php esc_html_e( 'Posts navigation', 'thirdrock' ); ?></h1>
<?php echo $links; ?>
</nav><!-- .navigation -->
<?php
endif;
}
endif;
if ( ! function_exists( 'thirdrock_paging_nav_rel' ) ) :
/**
* Display navigation to next/previous set of posts when applicable.
*
* @return void
*/
function thirdrock_paging_nav_rel($partnersLoop) {
// Don't print empty markup if there's only one page.
if ( $partnersLoop->max_num_pages < 2 ) {
return;
}
$paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
$pagenum_link = html_entity_decode( get_pagenum_link() );
$query_args = array();
$url_parts = explode( '?', $pagenum_link );
if ( isset( $url_parts[1] ) ) {
wp_parse_str( $url_parts[1], $query_args );
}
$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
$pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
$format = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
$format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';
// Set up paginated links.
$links = paginate_links( array(
'base' => $pagenum_link,
'format' => $format,
'total' => $partnersLoop->max_num_pages,
'current' => $paged,
'mid_size' => 2,
'add_args' => array_map( 'urlencode', $query_args ),
'prev_text' => '<i class="fa fa-chevron-left"></i>',
'next_text' => '<i class="fa fa-chevron-right"></i>',
'type' => 'list',
) );
if ( $links ) :
?>
<nav class="navigation paging-navigation" role="navigation">
<?php echo $links; ?>
</nav><!-- .navigation -->
<?php
endif;
}
endif;
if ( ! function_exists( 'thirdrock_post_nav' ) ) :
/**
* Display navigation to next/previous post when applicable.
*
* @return void
*/
function thirdrock_post_nav() {
// Don't print empty markup if there's nowhere to navigate.
$previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
$next = get_adjacent_post( false, '', false );
if ( ! $next && ! $previous ) {
return;
}
?>
<nav class="navigation post-navigation" role="navigation">
<div class="post-nav-box clear">
<h1 class="screen-reader-text"><?php esc_html_e( 'Post navigation', 'thirdrock' ); ?></h1>
<div class="nav-links">
<?php
previous_post_link( '<div class="nav-previous"><div class="nav-indicator">' . esc_html_x( 'Previous Post:', 'Previous post', 'thirdrock' ) . '</div>%link</div>', '%title' );
next_post_link( '<div class="nav-next"><div class="nav-indicator">' . esc_html_x( 'Next Post:', 'Next post', 'thirdrock' ) . '</div>%link</div>', '%title' );
?>
</div><!-- .nav-links -->
</div><!-- .post-nav-box -->
</nav><!-- .navigation -->
<?php
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment