Skip to content

Instantly share code, notes, and snippets.

@fdaciuk
Created January 29, 2013 14:53
Show Gist options
  • Save fdaciuk/4664822 to your computer and use it in GitHub Desktop.
Save fdaciuk/4664822 to your computer and use it in GitHub Desktop.
Paginação para WP
<?php
/**
* @param $query - (required) Query utilizada para o loop. Se for uma query personalizada, passe o objeto. Senão, passe $wp_query
* @param $baseURL - (optional) URL a partir de onde a paginação deve ser feita.
*
*/
function pagination( $query, $baseURL = '' ) {
if ( ! $baseURL ) $baseURL = get_bloginfo( 'url' );
$page = $query->query_vars["paged"];
if ( !$page ) $page = 1;
$qs = $_SERVER["QUERY_STRING"] ? "?".$_SERVER["QUERY_STRING"] : "";
// Only necessary if there's more posts than posts-per-page
if ( $query->found_posts > $query->query_vars["posts_per_page"] ) {
echo '<ul class="paginate">';
// Previous link?
if ( $page > 1 ) {
echo '<li class="previous"><a href="'.$baseURL.'page/'.($page-1).'/'.$qs.'">&lsaquo; Anterior</a></li>';
}
// Loop through pages
for ( $i=1; $i <= $query->max_num_pages; $i++ ) {
// Current page or linked page?
if ( $i == $page ) {
echo '<li class="active">'.$i.'</li>';
} else {
echo '<li><a href="'.$baseURL.'page/'.$i.'/'.$qs.'">'.$i.'</a></li>';
}
}
// Next link?
if ( $page < $query->max_num_pages ) {
echo '<li><a href="'.$baseURL.'page/'.($page+1).'/'.$qs.'">Próxima &rsaquo;</a></li>';
}
echo '</ul>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment