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>%s</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>%s</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>%s</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:
function custom_numeric_posts_nav( $query = '' ) {
if(!$query)
$query = $GLOBALS['wp_query'];
/** Stop execution if there's only 1 page */
if( $query->max_num_pages <= 1 )
return;
// ... and change the rest of the function accordingly ...
}
And this when you call it:
<!-- Call custom numbered pages -->
<?php custom_numeric_posts_nav( $my_download_query ); ?>
Related
I need to add pagination to transient pages now when a have 9 pages it looks like this:
1 2 3 ... 9
I would like to give a middle point between 3 and 9, i.e.
1 2 3 ... 6 ... 9
I also want the same middle subpage to be when I'm on the last / middle page. But this should be done by yourself knowing how to take the first step.
All my ideas fail. Does anyone have an idea for such a solution?
Full working code here:
<?php
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 class="prev_post">%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>%s</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>%s</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>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li class="next_post">%s</li>' . "\n", get_next_posts_link('>') );
echo '</ul></div>' . "\n";
A general pagination codeflow might look like the following:
<?php
function getPageDisplayArray(int $currentPage, int $totalCount, int $pageLen = 10): array
{
$paginationArr[$currentPage] = 1;
$totalPagesToDisplay = floor(($totalCount) / $pageLen);
//Get preceding pages logic
$pagesBefore = $currentPage - 1;
$iBefore = 2; //Number of pages you want befpre the current page
while($pagesBefore > 0 && $iBefore > 0) {
$paginationArr[$pagesBefore--] = 0;
$iBefore--;
}
//Get anteceding pages
$pagesAfter = $totalPagesToDisplay - ($pagesBefore + 1);
$iAfter = min([$pagesAfter, 2]); //Number of pages you want after your current page
while(-$iAfter < 0) {
//Either return the two pages after or don't if there are no pages after
$paginationArr[$iAfter + $currentPage] = 0;
$iAfter--;
}
if($pagesAfter > 2) {
//Calcluate Midpoint
$midpoint = floor($totalPagesToDisplay / 2) + 1;
if(!isset($paginationArr[$midpoint])) {
$paginationArr[$midpoint] = 0;
}
}
//Link the first page always if it hasn't already been set
if(!isset($paginationArr[1])) {
$paginationArr[1] = 0;
}
//Link the final page if it has not already been set.
if(!isset($paginationArr[$totalPagesToDisplay])) {
$paginationArr[$totalPagesToDisplay] = 0;
}
ksort($paginationArr);
return $paginationArr;
}
Where the Current Page, page length, and Total count are your parameters. The here will show two pages on either side as Display pages, as well as the first, last, and middle pages of your set for any arbitrart set. This will return a config array which sets the "Active" page with a value of one, and all of the others with a key of their page number and a value of zero. The rest would be a matter of displaying the values in your layout as you see fit.
This question already has answers here:
Wordpress: Check if there are previous posts before displaying link
(4 answers)
Closed 5 years ago.
I am trying to get a pagination on the index.php working. What I come up with yet looks something like this:
<nav>
Previous Page
Next Page
</nav>
However, I'd like to show the Previous/Next buttons only, when there actually exists a previous/next page. How can I achieve that?
Thanks!
Put this where you want to display previous/next page
<?php wpbeginner_numeric_posts_nav(); ?>
and in functions.php add
function wpbeginner_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('<i class="fa fa-angle-left"></i>') );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s>%s</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>%s</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>%s</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('<i class="fa fa-angle-right"></i>') );
echo '</ul></div>' . "\n";
}
Source: WPBegginer
My navigation stoped working on my custompost category... i have this :
Portfolio
<?php $loop = new WP_Query( array( 'post_type' => 'portfolios', 'posts_per_page' => 8, 'offset'=> 0 ,'paged' => $paged, ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li class="list_c">
<a href="<?php the_permalink(); ?>"><div><div id="over2"><p>+info</p></div></div>
<div id="read"> <span>ler mais </span></div>
<div id="ptxL" class="filldiv">
<div id="thumbL" class="grow "><?php the_post_thumbnail('featuredImageCropped3');?></div>
<div id="infoL">
<div id="titled" class="tt">
<h2><?php the_title();?></h2></div>
<div id="contentL"><h3><?php the_excerpt(); ?> </h3></div></div><div id="color"></div>
<!-- You have not associated any custom fields with this post-type. Be sure to add any desired custom fields to this post-type by clicking on the "Manage Custom Fields" link under the Custom Content Type menu and checking the fields that you want. -->
</div></a></li>
<?php endwhile; wp_reset_query(); ?>
<?php wpbeginner_numeric_posts_nav(); ?>
<?php get_footer(); ?>
and on my functions.php:
function wpbeginner_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 class="pre">%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>%s</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>%s</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>%s</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";
}
i changed servers maybe thats the problem...but i can make it work...my debug finds no errors...
Please try this code.I think you have mistake in paged parameter.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'portfolios', 'posts_per_page' => 8, 'offset'=> 0 ,'paged'=>$paged ) ); ?>
query_posts($args);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li class="list_c">
<a href="<?php the_permalink(); ?>"><div><div id="over2"><p>+info</p></div></div>
<div id="read"> <span>ler mais </span></div>
<div id="ptxL" class="filldiv">
<div id="thumbL" class="grow "><?php the_post_thumbnail('featuredImageCropped3');?></div>
<div id="infoL">
<div id="titled" class="tt">
<h2><?php the_title();?></h2></div>
<div id="contentL"><h3><?php the_excerpt(); ?> </h3></div></div><div id="color"></div>
<!-- You have not associated any custom fields with this post-type. Be sure to add any desired custom fields to this post-type by clicking on the "Manage Custom Fields" link under the Custom Content Type menu and checking the fields that you want. -->
</div></a></li>
<?php endwhile;endif; ?>
<?php wpbeginner_numeric_posts_nav(); ?>
<?php wp_reset_query(); ?>
I hope this will work for you.
Thanks
I'm trying to generate pagination URL for first page. I used the following code.
get_pagenum_link(1); // gives http://www.website.com/
anyhow,
get_pagenum_link(2); // gives http://www.website.com/paged/2/
get_pagenum_link checks for the page # if($paged == 1) {// Don't include Page# in URL } I want to disable this check so that, it always include page #in URL
Also, http://www.website.com/paged/1/ is redirected to http://www.website.com/ I added a filter
add_filter('redirect_canonical','pif_disable_redirect_canonical');
function pif_disable_redirect_canonical($redirect_url) {
return false;
}
but, I think, its not a right way to do, it'll stop all canonical redirections.
Add this function into theme’s functions.php
function custom_pagination() {
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>%s</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>%s</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>%s</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";
}
Usage :
To call this function you have to add the below php code in your theme’s index.php, archive.php, category.php, or any other page template.
<?php custom_pagination(); ?>
Pagination Doesn't work with word press custom post.
i have tried these code below,but pagination doesn't appear.
tried different query but no result.
i am new in php and word press.i just copy and past code.
can anyone please help me?
what i have done so far are below.
in function.php
/*pagination*/
function wpbeginner_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>%s</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>%s</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>%s</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";
}
in custom post query
<?php
global $post;
$args = array( 'posts_per_page' => 5, 'post_type'=> 'latestnews');
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
?>
<div class="page_news">
<div class="single_page_news">
<h2><?php the_title(); ?><h2>
<p><?php the_content(); ?></p>
</div>
</div>
<?php endforeach; ?>
<?php wpbeginner_numeric_posts_nav(); ?>
please help me
Use These code
$paged = ( get_query_var('page') ) ? get_query_var('page') :1;
$query = new WP_Query( array( 'posts_per_page' => 1,'paged' => $paged,'post_type' => 'achievements','orderby' => 'date', 'order' => 'ASC' ) );
while ( $query->have_posts() ) : $query->the_post();
endwhile;
Instead of
$args = array( 'posts_per_page' => 5, 'post_type'=> 'latestnews');
$myposts = get_posts( $args );
Follow this link
http://thenetapp.com/2014/01/how-to-list-wordpress-posts-with-pagination/
Your pagination function is only set up for the default main query, not for a custom query.
Also, don't use get_posts for paginated queries. It it is nice function to use for a custom query, but it becomes a bummer to work with once you require pagination.
Rather use WP_Query for paginated queries, it is much easier to use.
Example:
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
$args = array(
'posts_per_page' => 1,
'paged' => $paged,
'post_type' => 'YOUR POST TYPE'
);
$q = new WP_Query($args);
if($q->have_post()) {
while($q->have_posts()) {
$q->the_post();
//YOUR LOOP
}
//YOUR PAGINATION
}
wp_reset_postdata();
You can have a look at the codex for extra parameters.
You now need to change every instance of $wp_query in your pagination function to $q for it to work.
Just a point of note, you don't have to call the $post global
EDIT
From your comments, there is a much easier way to accomplish your goal without any custom query
This page is an archive page that is meant to display your custom post type latestnews. You can simply just rename your archive-custom.php to archive-latestnews.php. See the Template Hierarchy. Just make sure has_archive is set to true when you register your post type
You should also never shop the main query for a custom query on any type of archive page. It is always troublesome, as you can see. So, delete your custom query and replace it with the default query
This is all you should have in your archive page
if(have_post()) {
while(have_posts()) {
the_post();
//YOUR LOOP
}
//YOUR PAGINATION
}
Just change all the instances of $q back to $wp_query again. Everything should work then
For extra info, you have to check out this post I've done on WPSE