Wordpress - Custom Taxonomy Pagination

I use:

  • Wordpress 3.4
  • WP-PageNavi 2.82

I am registering a custom taxonomy (directory)

<?php add_action('init', 'pca_register_taxonomy', 0); function pca_register_taxonomy() { register_taxonomy('catalog', null, array( 'label' => __('Catalogs', __), 'labels' => array( 'name' => __('Catalogs', __), 'singular_name' => __('Catalog', __), 'search_items' => __('Search Catalogs', __), 'popular_items' => __('Popular Catalogs', __), 'all_items' => __('All Catalogs', __), 'parent_item' => __('Parent Catalog', __), 'parent_item_colon' => __('Parent Catalog', __), 'edit_item' => __('Edit Catalog', __), 'update_item' => __('Update Catalog', __), 'add_new_item' => __('Add New Catalog', __), 'new_item_name' => __('New Catalog Name', __), 'separate_items_with_commas' => __('Separate catalogs with commas', __), 'add_or_remove_items' => __('Add or remove catalogs', __), 'choose_from_most_used' => __('Choose from the most used catalogs', __), 'menu_name' => __('Catalogs', __) ), 'public' => true, 'show_in_nav_menus' => true, 'show_ui' => true, 'show_tagcloud' => false, 'hierarchical' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'catalog', 'with_front' => true, 'hierarchical' => true ), 'capabilities' => array( 'manage_terms', 'edit_terms', 'delete_terms', 'assign_terms' ) ) ); } ?> 

I am registering a custom post type (product)

 <?php add_action('init', 'pca_register_post_type'); function pca_register_post_type() { register_post_type('product', array( 'label' => __('Products', __), 'labels' => array( 'name' => __('Products', __), 'singular_name' => __('Product', __), 'add_new' => __('Add New', __), 'add_new_item' => __('Add New Product', __), 'edit_item' => __('Edit Product', __), 'new_item' => __('New Product', __), 'all_items' => __('All Products', __), 'view_item' => __('View Product', __), 'search_items' => __('Search Products', __), 'not_found' => __('No products found', __), 'not_found_in_trash' => __('No products found in Trash', __), 'parent_item_colon' => '', 'menu_name' => __('Products', __) ), 'description' => '', 'public' => true, 'exclude_from_search' => false, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_nav_menus' => true, 'show_in_menu' => true, 'show_in_admin_bar' => true, 'menu_position' => 20, 'capability_type' => 'post', 'meta_cap' => true, 'hierarchical' => false, 'supports' => array( 'title', 'editor', 'thumbnail', 'page-attributes', 'post-formats' ), 'taxonomies' => array('catalog'), 'has_archive' => false, 'rewrite' => array( 'slug' => 'products', 'with_front' => true, 'feeds' => false, 'pages' => true ), 'query_var' => true, 'can_export' => true ) ); } ?> 

Then I create a new file for tax -> taxonomy-catalog.php

In this file, I request all products (custom message type) from the specified directory (tax):

 <?php $paged = get_query_var('paged'); $paged = ($paged) ? $paged : 1; $products = new WP_Query(array( 'catalog' => $catalog_data->slug, // $catalog_data is the current taxonomy (woman) 'post_type' => 'product', 'posts_per_page' => 12, 'paged' => $paged )); ?> <?php while ($products->have_posts()) : $products->the_post(); ?> // Show title, content ... everything ok <?php endwhile; ?> <?php if (function_exists('wp_pagenavi')) wp_pagenavi(array('query' => $products)); ?> <?php wp_reset_postdata(); ?> 

The page display is displayed correctly, but when I click on page 2 or more, I have a 404 error.

  • Jobs → mywebsite.com/catalog/woman
  • Doesn't work → mywebsite.com/catalog/woman/page/2

I have already updated permalinks.

Any idea to fix this? thanks

+4
source share
6 answers

Put this in your "functions.php" and then restore the permalinks. He works with 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 replacing "paged" with "page" in the rewrite rule for your custom taxonomy.

This is my first contribution here. I hope I help you.

+1
source

As soon as I ran into a problem about it and went through hard times hrs to hrs, pulling my hair. I googled and did not find any specific solution on topics. I found several talent articles, but they did not satisfy my problems. In fact, a custom taxonomy page for archiving pages depends on some parameters of the arguments to related functions. So I'm really going to share my thoughts on solving the problem of pagination taxonomy.

Five things you need for a custom taxonomic page page that works just fine:

(1) Do not set the exclude_from_search parameter key as the register_post_type argument parameter, or if you mention it, set it to 'exclude_from_search' => false . By default, it is set to false if not specified.

(2) The taxonomy that will be used with the custom set of message types 'taxonomies' => 'custom_taxonomy_name' as a parameter to the register_post_type argument or
use register_taxonomy_for_object_type() directly. Custom taxonomies still need to be registered with register_taxonomy() .

(3) When querying in 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 the posts_per_page and paged parameter in the argument array new WP_Query($arg) . If you do not set the static front page , then you must use the page parameter in the array of arguments new WP_Query ($arg)

(4) Use the paginate_links( $args ) Wordpress function as shown below to render pagination in the 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 lists ul li with the class page-numbers . If you use bootstrap inject pagination class for ul using javascript or jquery and you get a nice attractive pagination.

Hopefully now you can enjoy pagination in your taxonomy archive template without any 404 problem :-)

+1
source

Basically that

 get_query_var('page') 

which contains the page number

from 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); 
0
source

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();?> 
0
source


When you create any kind of CPT, you need to add this feature.

 flush_rewrite_rules(); 
It will manage / tune / reassign your memory.
So please add this line, your code will work.
Precautionary measures.
 1: Add this function : flush_rewrite_rules(); 
2:Your pagination function (wp_pagenavi()) will call just below the endwhile;
I am sure your pagination will work and let me know if this does not work.
Thank you.
0
source

What helped me was to set the "Maximum number of blog pages" in the reading settings to 1. Then it works.

With 10 by default, it throws error 404 on page 2. On page 1, everything is perfect.

Therefore, given this, the solution is to set "Blog Pages No More" to 1 only for those taxonomies or categories that you need. The code that you must place inside functions.php is here: https://forums.envato.com/t/wordpress-custom-page-type-taxonomy-pagination/76549/12

0
source

All Articles