Firstly, you should never use query_posts
to construct custom queries. This is not just my emphasis, but the codex as well. The one big problem with query_posts
is, it many circumstances, pagination fails
Note: This function isn’t meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).
Just a point of note, your next_posts_link
$max_pages
parameter should be set for custom queries, otherwise it will fail
You should always construct custom queries with WP_Query
. So your custom query should look like this
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'paged' => $paged,
'orderby' => 'ID',
'order' => 'DESC',
'cat' => 50,
'posts_per_page' => 20,
'post_status' => 'publish',
'suppress_filters' => true
);
$blogPosts = new WP_Query( $args );
if ( $blogPosts->have_posts() ) : while ( $blogPosts->have_posts() ) : $blogPosts->the_post();
<----YOUR LOOP---->
endwhile;
// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $blogPosts->max_num_pages );
previous_posts_link( 'Newer Entries' );
wp_reset_postdata();
endif;
Lastly, your counter should start outside the loop, not inside
if ( $blogPosts->have_posts() ) :
$count = 0;
while ( $blogPosts->have_posts() ) : $blogPosts->the_post();
EDIT
Custom pagination function. I don’t know who the original author of this function is, but thanks to you if you ever find this answer 🙂
function custom_pagination($pages="", $range = 2) {
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
$string = _x( 'Page %1$s of %2$s' , '%1$s = current page, %2$s = all pages' , 'pietergoosen' );
echo "<div class="pagination"><span>" . sprintf( $string, $paged, $pages ) . "</span>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href="".get_pagenum_link(1)."">" . __( '« First', 'pietergoosen' ) . "</a>";
if($paged > 1 && $showitems < $pages) echo "<a href="".get_pagenum_link($paged - 1)."">" . __( '‹ Previous', 'pietergoosen' ) . "</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href="".get_pagenum_link($i)."" class=\"inactive\">".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href="" . get_pagenum_link($paged + 1)."">" . __( 'Next ›', 'pietergoosen' ) . "</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href="".get_pagenum_link($pages)."">" . __( 'Last »', 'pietergoosen' ) . "</a>";
echo "</div>\n";
}
} //