Custom Taxonomy Pagination Not Functioning - php

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' => '»'
)
);
?>

Related

pagination for woocommerce account downloads page

i am completely customizing the downloads page, i use the following code on downloads page, it shows the page number correctly, but when i go to other pages, it gives the same results as the first page
path : /themes/theme-name-child/woocommerce/myaccount/downloads.php
<?php
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'posts_per_page' => 6,
'paged' => $paged,
);
$the_query = new WP_Query( $args );
$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' => $the_query->max_num_pages
) );
?>
i searched a lot, but i didn't find anything, i need your help

Another problem with 404 in pagination WooCommerce

I read so many articles and didnt find answer.
my custom product loop in category:
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
global $post;
$args = array(
'posts_per_page' => 6,
'product_cat' => $post->post_name, // **The father category**
'post_type' => 'product',
'paged' => $paged,
'page' => $page,
'pagination' => true
);
$loop = new WP_Query( $args );
echo $loop->request;
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
?>
////////////////// product info//////////////
<?php endwhile;
$big = 999999999; // need an unlikely integer
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
) );
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
in category page i have same link:
http://localhost:3000/product-category/cats/videocard/
pagination link:
http://localhost:3000/product-category/cats/videocard/page/2/
but i have 404 on it.
my permalink settings:
enter image description here
my "Read" settings:
enter image description here
What i do wrong?
Possibly your problem is related to the WP_Query arguments. I have spotted the following issues:
the 'pagination' is not a valid argument,
the taxonomy argument ( 'product_cat' ) given as key is deprecated, you should use 'tax_query' instead.
the $page variable value is not defined ( at least not in your sample script ) and it seems that the definition of both 'page' and 'paged' arguments might cause conflicts to your query.
Try using the following:
global $post;
$args = array(
'posts_per_page' => 6,
'post_type' => 'product',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $post->post_name // !
),
),
);
$loop = new WP_Query( $args );
// echo $loop->request; - I guess this is a test left-over
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
/**
* No need to call the $product global within the loop,
* use the $loop->post or even better call the product object
* using the WooCommerce wc_get_product() function as displayed
* below
*/
// global $product;
$product = wc_get_product( $loop->post );
?>
////////////////// product info//////////////
<?php endwhile;
$big = 999999999; // need an unlikely integer
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
) );
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
Last, but not least, the taxonomy term value you are giving for product_cat ( commented as the father category ) is the current post slug. I understand why you might want to do this, but under some circumstances, this could be also the reason you are getting these 404s. Therefore, if the above script is not solving your problem, I would advise you to look into this too.

Wordpress custom post type archive pagination results in 404 error

Whenever I try to access another page using the archive's pagination I get a 404 error. Apparently a well known problem but so far no solution has worked in my case. I might have been applying it wrong though.
Following advice I found online and in the codex I currently have the archive page template set up for a custom post-type like so:
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'post_type' => 'application',
'posts_per_page' => 2,
'paged' => $paged
);
$query = new WP_Query($args);
if ($query->have_posts()) { ?>
<div class="posts">
<!-- THE POSTS -->
</div>
<?php
$big = 999999999; // unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Previous', 'textdomain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Next', 'textdomain' ) ),
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages
) );
wp_reset_postdata();
} ?>
and have my permalinks set to /%category%/%postname%/. I understand this could be part of the problem.
And this is where I kind of get lost. What am I doing wrong? Any help would be very much appreciated.
I would change the first line to:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1 ?>
Also adding this line after that first line might help:
$number_of_posts = get_option('posts_per_page', 2);

Pagination links is not working in wordpress

Pagination links are not working in my blog post in woocommerce...
when i click 2 the second page http://www.freshcropmushrooms.com.au/blog/page/2 its redirect to the http://www.freshcropmushrooms.com.au/blog/
on the otherhand next link is also not working
I use the following code in pagination.php
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $wp_query;
if ( $wp_query->max_num_pages <= 1 )
return;
?>
<nav class="woocommerce-pagination">
<?php
echo paginate_links( apply_filters( 'woocommerce_pagination_args', array(
'base' => str_replace( 999999999, '%#%', get_pagenum_link( 999999999 ) ),
'format' => '',
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $wp_query->max_num_pages,
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3
) ) );
?>
</nav>
Please fix it for me
pagination doesn't work correctly with queries unless they're on a page.
you might need to re-read that part. what query_post() does is destroy the standard query.
It would be like
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args= array(
'category_name' => 'News',
'paged' => $paged //replace it with your aray key
);
query_posts($args);

Styling pagination in twenty twelve theme

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' );
?>

Categories