Created
June 11, 2014 09:08
-
-
Save gistfrojd/fd7828be12e5a760efa3 to your computer and use it in GitHub Desktop.
Wordpress - pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function pagination() { | |
// 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' => 1, | |
'add_args' => array_map( 'urlencode', $query_args ), | |
'prev_next' => false, | |
'type' => 'array' | |
)); | |
if ( $links ) : | |
?> | |
<nav> | |
<ul> | |
<?php foreach ($links as $link): ?> | |
<li> | |
<?php echo $link; ?> | |
</li> | |
<?php endforeach; ?> | |
</ul> | |
</nav> | |
<?php | |
endif; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment