I\'m using:
I register custom taxonomy (catalog)
Once I have faced problem about this and passed hard times hrs to hrs by pulling hair. I googled and didn't found any specific solution about the topics. I found several talents' article but they weren't satisfying my problems. Actually custom taxonomy archive page pagination is depends on some settings of arguments of related functions. So I am actually going here sharing my thoughts of solving the taxonomy archive pagination problem.
Five things you need for custom taxonomy archive page pagination working perfectly :
( 1 ) Don't put exclude_from_search
parameter key as register_post_type
argument parameter or
if mention set it 'exclude_from_search' => false
. By default it is set false
if not mentioned.
( 2 ) The taxonomy that will be use with the custom post type set
'taxonomies' => 'custom_taxonomy_name'
as register_post_type
argument parameter or
use register_taxonomy_for_object_type()
directly.
Custom taxonomies still need to be registered with register_taxonomy()
.
( 3 ) While querying within new WP_Query ($args)
i ) If not set in admin `static front page` use before `new WP_Query($args)`
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
and use $query = new WP_Query( array( 'paged' => $paged ) );
ii ) If set in admin static front page use before 'new WP_Query($args)'
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
and use $query = new WP_Query( array( 'page' => $paged ) );
Remember to use posts_per_page
and paged
parameter in new WP_Query($arg)
argument array.
If not set static front page
then you should use page
parameter in new WP_Query ($arg)
argument array
( 4 ) Use Wordpress paginate_links( $args )
function like the example below to render pagination in archive template file.
<?php $big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%', or '/paged=%#%', // if using pretty permalink
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages ) ); // Here $max_num_pages is the properties of new WP_Query() object . It is total number of pages. Is the result of $found_posts / $posts_per_page
?>
( 5 ) The paginate_links()
function output the ul li
listing with page-numbers
class.
If you use bootstrap inject pagination
class to the ul
with the help of javascript or jquery and a nice fancy pagination will be output.
Hope you can now enjoy pagination in the taxonomy archive template without any 404 problem :-)
Put this into your "functions.php" and then regenerate permalinks. It works to me!
function taxonomy_rewrite_fix($wp_rewrite) {
$r = array();
foreach($wp_rewrite->rules as $k=>$v){
$r[$k] = str_replace('catalog=$matches[1]&paged=','catalog=$matches[1]&page=',$v);
}
$wp_rewrite->rules = $r;
}
add_filter('generate_rewrite_rules', 'taxonomy_rewrite_fix');
The key is to replace "paged" to "page" into the rewrite rule for your custom taxonomy.
This is my first contribution here. Hope I help you.
Use query_posts instead of wp_query. It should work.
This is my Code.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'paged' => $paged,
'orderby' => 'asc',
'tax_query' => array(
array(
'taxonomy' => "$term->taxonomy",
'terms' => "$term->name",
'field' => 'slug'
)
)
);
$query = new WP_Query( $args );
if($query->have_posts() ) :
// your code
?>
<?php endwhile; endif;
wp_pagenavi();?>
When you create any CPT then you need to add this function.
flush_rewrite_rules();
It will manage/adjust/reassign your memory.1: Add this function : flush_rewrite_rules();
2:Your pagination function (wp_pagenavi()) will call just below the endwhile;
I 'm sure,your pagination will work and let me know if it will not work.Basically that's
get_query_var('page')
that holds the page number
from the working code:
global $query_string;
$page_index = max(1, get_query_var('page'));
parse_str($query_string, $queryParams);
$queryParams['paged'] = $page_index;
$queryParams['posts_per_page'] = $posts_per_page;
query_posts($queryParams);
What helped me is to set "Blog pages show at most" in Reading Settings to 1. Then it works.
With the default 10, it throws 404 error on page 2. On page 1 it's all perfect.
So given this works, the solution is to set "Blog pages show at most" to 1 only for those taxonomies or categories you need. The code you should place inside functions.php is here: https://forums.envato.com/t/wordpress-custom-page-type-taxonomy-pagination/76549/12