I am trying to add pagination to product i am getting pagination link eg '1,2 next>>' but when i click on the link it show page not found.
$args = array( 'post_type' => 'product','product_cat' => 'clothing','posts_per_page' => '1', 'order' => 'DESC','paged' => $paged );
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
echo $product->get_regular_price();
endwhile;
$total_pages = $loop->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
wp_reset_postdata();
}
I think you should change format to:
'format' => '?paged=%#%',
Because current 'format' (if base is correct) gives you for example https://yourwpwebsite.com/basepage/page/1 but it should be https://yourwpwebsite.com/basepage?page=1 so it is still the same page but with page data passed in URL
I am not sure if current 'base' is correct, but you can also try changing it to:
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
echo $product->get_regular_price();
endwhile;
$total_pages = $loop->max_num_pages;
$big = 999999999; // need an unlikely integer
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
wp_reset_postdata();
}
Try this, if it is not working, provide more details and current HTML output.
Related
I have set up CPT with a custom taxonomy, yet I cannot get pagination working. First page renders fine but when I click through to "/page/2/", I get a 404 error.
I have a CPT named "species_guide" and a custom taxonomy named "species".
I attempted the solution outlined here: https://wpza.net/how-to-paginate-a-custom-post-type-in-wordpress/ and https://wpza.net/how-to-a-paginate-custom-taxonomy-archive-in-wordpress/ but to no avail.
Below is my code, any input would be appreciated:
in taxonomy-species.php I have the following:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$term_slug = get_query_var('species');
$args = array(
'post_type' => 'species_guide',
'tax_query' => array(
array(
'taxonomy' => 'species',//custom taxonomy name
'field' => 'slug',
'terms' => $term_slug
)
),
'posts_per_page' => 10,
'paged' => $paged
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( 'templates/post-archive' );
endwhile;
?>
<div class="pagination">
<?php
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages,
'prev_text' => '«',
'next_text' => '»'
) );
wp_reset_postdata();
?>
and then in functions.php I have the following
function custom_tax_query_change( $query ) {
if ( ! is_admin() && $query->is_tax( 'species' ) ) {
$query->set( 'posts_per_page', 10 );
}
}
add_action( 'pre_get_posts', 'custom_tax_query_change' );
Should I be using the different species slugs in the is_tax() function? Not sure why I can't get this working
Your taxonomy template name should be taxonomy-species.php to match the taxonomy name, see WordPress documentation
You don't need to create a new instance of WP_Query() for the loop, simply copy existing taxonomy.php or archive.php file and name the file as described above. Make HTML/PHP changes as needed.
Here is the updated snippet for pagination:
<?php
// for total pages in the loop.
global $wp_query;
$big = 999999999;
echo 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_text' => '«',
'next_text' => '»'
)
);
?>
i have a problem with my pagination, it doesn't seem to work when i click on page 3 or 4, when it clearly says i have 4 pages when i do $the_query->max_num_pages. I dont understand what the problem is. This is my code:
$big = 999999999; // need an unlikely integer
$translated = __( 'Page', 'mytextdomain' ); // Supply translatable string
var_dump($the_query->max_num_pages);
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => floor($the_query->max_num_pages),
'before_page_number' => '<span class="screen-reader-text">'.$translated.' </span>'
) );
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
// The Query
$the_query = new WP_Query([
$atts,
'posts_per_page' => 6,
'post__not_in'=> get_option('sticky_post'),
'ignore_sticky_posts' => 1,
'paged' => $paged,
]);
This is driving me crazy. I've tried every solution I could find but still not working for me.
I have a custom post type.
/**
* Videos Custom Post Type
*/
function videos_post_type() {
register_post_type( 'videos', array(
'labels' => array(
'name' => 'Videos',
'singular_name' => 'Video',
'add_new_item' => 'Add New Video',
'add_new' => 'Add New Video',
'edit_item' => 'Edit Video',
'new_item' => 'New Video',
'all_items' => 'All Videos'
),
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'videos' ),
'menu_icon' => 'dashicons-format-video'
));
}
add_action( 'init', 'videos_post_type' );
Everything is working fine except pagination on the 'Archive' page. Pagination links are showing up properly but when I click on "Next", it ends in page not found error.
<div class="ast-row">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query( array(
'post_type' => 'videos',
'posts_per_page' => 1,
'orderby'=> 'menu_order',
'paged'=> $paged
) );
while ( $loop->have_posts() ) {
$loop->the_post(); ?>
<div class="ast-col-md-4">
<?php echo the_content(); ?>
</div>
<?php } ?>
</div>
<div class="ast-row">
<?php
echo paginate_links( array(
'total' => $loop->max_num_pages
) );
?>
<?php wp_reset_postdata(); ?>
</div>
Also tried the following to echo_paginate_links()
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages
) );
Moreover-
* I have tried to save permalink
* I have tried to change permalink structure
* I have tried query_posts instead of WP_Query
---none of these worked.
I should also mention that I have not created a single-videos.php template (because I don't need it for this post type) but I don't think that has anything to do with this issue. Please correct me if I am wrong. I just can't get my head around it. I'm using the Astra theme if that helps.
Please share your suggestions. Thanks!
Try below (replace your html)
<?php
global $loop;
$query = $query ? $query : $loop;
$big = 999999999;
$paginate = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'type' => 'array',
'total' => $query->max_num_pages,
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'prev_previous' =>('← Older posts'),
'prev_next' => ( 'Newer posts →' ),
'prev_text' => __('«'),
'next_text' => __('»'),
));
if ($query->max_num_pages > 1) :
?>
<div class="paginate">
<ul class="pagination">
<?php
foreach ( $paginate as $page ) {
echo '<li>' . $page . '</li>';
}
?>
</ul>
</div>
<?php endif; ?>
Replace
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages
) );
With
global $loop;
$query = $query ? $query : $loop;
$big = 999999999;
$paginate = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'type' => 'array',
'total' => $query->max_num_pages,
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'prev_previous' =>('← Older posts'),
'prev_next' => ( 'Newer posts →' ),
'prev_text' => __('«'),
'next_text' => __('»'),
));
if ($query->max_num_pages > 1) :
If Not Works I have Another Solusationn Just Ask me
I had the same problem recently. Not sure what the problem was but this worked for me.
$pagination = [
'base' => #add_query_arg('paged','%#%'),
'format' => '',
'total' => $the_query->max_num_pages,
'current' => get_query_var('paged') ?? 1,
'show_all' => false,
'mid_size' => 4,
'type' => 'list',
'next_text' => '»',
'prev_text' => '«'
];
echo paginate_links($pagination);
I got the same issue and just figured out that the cause of my problem is that I had set the same slug to my Custom Post Type and the page showing the new WP_Query.
Hope it will be helpful.
Pagination not working on category page, the query that i used on category page is below:
number of paginate is correct but while I am clicking on that number it redirect to home page where I am wrong please give me solution,
//code below
$cat_ID = get_query_var('cat');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$article = new WP_Query(array('post_type' => 'post','cat' => cat_ID,
'posts_per_page' => '2', 'paged' => $paged));
while ($article->have_posts()) : $article->the_post();
$post_id = get_the_ID();
// here is my pagination code
$big = 76;
$args = array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'total' => $article->max_num_pages,
'current' => $paged,
'prev_next' => True,
'prev_text' => __('Previous'),
'next_text' => __('Next'),
'type' => 'list');
echo paginate_links($args);
<?php while (have_posts()):the_post(); ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$slide = new WP_Query(array('post_type' => 'post', 'posts_per_page' => '12', 'paged' => $paged));
if ($slide->have_posts()) : while ($slide->have_posts()) : $slide->the_post();
$post_id = get_the_ID();
//show contents here
$big = 76;
$args = array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'total' => $slide->max_num_pages,
'current' => $paged,
'prev_next' => True,
'prev_text' => __('Previous'),
'next_text' => __('Next'),
'type' => 'list');
// ECHO THE PAGENATION
echo paginate_links($args);
hello friend i have faced this type of problem before and the solution that i came with was
Go to setting and then go to to reading tab
there you can see "Blog pages show at most" option
adjust the number according to your need or until the pagination starts working
Change 'format' parameter 'paged' to anything else.
echo paginate_links( array(
...
'format' => '?myparam=%#%',
Then access/get it from your url something like this
$page = (get_query_var('myparam')) ? get_query_var('myparam') : 1;
$slide = new WP_Query(array('post_type' => 'post', 'posts_per_page' => '12', 'paged' => $page));
$category_link = get_category_link( $category_id ) . '/%_%';
'base' => $category_link,
'format' => 'page/%#%',
'total' => $article->max_num_pages
I'm using WordPress twenty twelve theme as parent theme. I wanted to add numbered pagination to it so I found this code and I added it to my function.php file:
<?php
if ( ! function_exists( 'twentytwelve_content_nav' ) ) :
function twentytwelve_content_nav() {
global $wp_query;
$big = 999999999; // need an unlikely integer
echo 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
) );
}
endif;
?>
which really worked fine the problem is I failed to add css styling as It is not warped in any ID or class which limits my ability to style it the way I planed. Is there any way to add class or Id to this code so I can style it?
I have checked the Wordpress codec, and there is no way to add a class or id in the paginate_links function.
Why not just wrap the echo statement in a container div?
?><div class="paginated-links"><?php
echo 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
) );
?></div><?php
EDIT: To use this, replace the following code, with the code above:
echo 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
) );
<?php $category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
?>
<?php
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wpquery = new WP_Query(array(
'order' => 'DESC',
'cat' => $cat_id,
'posts_per_page' => 10,
'paged'=>$page
));
while ($wpquery->have_posts()) {
$wpquery->the_post();
?>
<li class="contentlist">
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(); ?></p>
</li>
<?php
}
?>
<?php
global $wpquery;
if( $wpquery->max_num_pages >1){
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wpquery->max_num_pages
) );
}
?>
functions.php
// Pagination for each category
function custom_ppp( $query ) {
if ( !is_admin() && $query->is_category() && $query->is_main_query() ) {
$query->set( 'posts_per_page', '10' );
}
}
add_action( 'pre_get_posts', 'custom_ppp' );
?>