I’m trying to get my pagination working with a custom downloads page on my site. I’m using Easy Digital Downloads as my store backend and I’m calling the store items in a custom wp_query. I have the same code working to paginate my archives, but for some reason, it’s not working here. I would greatly appreciate any help that can be offered. Here is my code:
From my functions.php file
/* Pagination numbers function
------------------------------------
Allows Google-like page numbers to post pages by calling function */
function custom_numeric_posts_nav() {
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li>%s</li>' . "n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s><a href="%s">%s</a></li>' . "n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>â¦</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s><a href="%s">%s</a></li>' . "n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>â¦</li>' . "n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s><a href="%s">%s</a></li>' . "n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li>%s</li>' . "n", get_next_posts_link() );
echo '</ul></div>' . "n";
}
MY Query
<?php
// Allow Pagination
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
// Arguments
$type = 'download';
$args=array(
'post_type' => $type,
'posts_per_page' => -1,
'posts_per_page' => 16,
'paged' => $paged
);
// Get results
$my_download_query = new WP_Query($args);
// The Loop
if( $my_download_query->have_posts() ) {
while ($my_download_query->have_posts()) : $my_download_query->the_post(); ?>
<div id="store-item">
<?php
// Check for thumbnail
if ( has_post_thumbnail() ) {
echo '<a href="' . the_permalink() . '" title="' . the_title_attribute() . '" >';
echo the_post_thumbnail('store-thumb');
echo '</a>';
}?>
<?php
$content = get_the_content();
echo string_limit_words($content,20) . '...';
?>
<!-- Get Price -->
<p class="price"><?php echo edd_price_range( $download_id ); ?></p>
<div class="call-to-action-button">
<a type="button" href="<?php the_permalink()?>">Details</a>
</div>
<?php edit_post_link(); ?>
</div> <!-- END #store-item -->
<?php endwhile; ?>
<!-- Call custom numbered pages -->
<?php custom_numeric_posts_nav(); ?>
<div class="navigation"><p><?php posts_nav_link(); ?></p></div>
<?php } ?> <!-- endif -->
?>
That’s because inside
custom_numeric_posts_nav()
you’re using the global variable$wp_query
, whereas$my_download_query
in your custom query.You could change
custom_numeric_posts_nav()
to accept the variable$my_download_query
as an optional parameter and, if passed, use that:And this when you call it: