问题
I coded custom loop within multiple loops structure:
$q = get_posts( $args );
// Run your loop
echo '<div class="row">';
$i = 0;
foreach ( $q as $post ) : setup_postdata( $post );
$i++;
if ($i%4==0)
echo '</div><div class="row">';
get_template_part('loop');
endforeach;
wp_bs_pagination();
wp_reset_postdata();
except for I added wp_bs_pagination();
to load pagination. It only repeat the same set of posts o every page. Any suggestions?
回答1:
Do not use get_posts()
for paginated queries. get_posts
works well for non-paginated queries, but not paginated queries.
The issue is, get_posts
only returns the $posts
property from WP_Query
and not the complete object. Furthermore, get_posts()
passes 'no_found_rows'=> true
to WP_Query
which legally breaks pagination.
Because get_posts
uses WP_Query
, we might as well use WP_Query
which returns everything we need to paginate our query. Just remember, we need to add the paged
parameter to the query in order to page it
We can rewrite your query as follow
$args= [
'paged' => get_query_var( 'paged' ),
// Add any additional arguments here
];
$q = new WP_Query( $args );
// Run your loop
if( $q->have_posts() ) {
echo '<div class="row">';
$i=0;
while ( $q->have_posts() ) {
$q->the_post();
$i++;
if($i%4==0)
echo '</div><div class="row">';
get_template_part('loop');
}
wp_bs_pagination();
wp_reset_postdata();
}
You will need to somehow pass $q->max_num_pages
to wp_bs_pagination()
to set pagination to your custom query, but I do not know the function, so I cannot give you an exact solution on this.
回答2:
Try this, paste this in your functions.php
function custom_pagination() {
global $wp_query;
$big = 999999999; // need an unlikely integer
$pages = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_next' => false,
'type' => 'array',
'prev_next' => TRUE,
'prev_text' => __('«'),
'next_text' => __('»'),
) );
if( is_array( $pages ) ) {
$paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
echo '<ul class="pagination">';
foreach ( $pages as $page ) {
echo "<li>$page</li>";
}
echo '</ul>';
}
}
and then use the function custom_pagination()
Got the solution from here: http://www.ordinarycoder.com/paginate_links-class-ul-li-bootstrap/
来源:https://stackoverflow.com/questions/34769194/wordpress-custom-loop-pagination