I’m using:
- WordPress 3.4
- WP-PageNavi 2.82
I register custom taxonomy (catalog)
<?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 register 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 query all products (custom post type) from specified catalog (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(); ?>
Pagination is displayed correctly but when I click on page 2 or over I have a 404 error.
- Works -> mywebsite.com/catalog/woman
- Works not -> mywebsite.com/catalog/woman/page/2
I already refreshed permalinks.
Any idea to fix this ? thanks
Put this into your “functions.php” and then regenerate permalinks.
It works to me!
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.
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 asregister_post_type
argument parameter orif mention set it
'exclude_from_search' => false
. By default it is setfalse
if not mentioned.( 2 ) The taxonomy that will be use with the custom post type set
'taxonomies' => 'custom_taxonomy_name'
asregister_post_type
argument parameter oruse
register_taxonomy_for_object_type()
directly.Custom taxonomies still need to be registered with
register_taxonomy()
.( 3 ) While querying within
new WP_Query ($args)
Remember to use
posts_per_page
andpaged
parameter innew WP_Query($arg)
argument array.If not set
static front page
then you should usepage
parameter innew WP_Query ($arg)
argument array( 4 ) Use WordPress
paginate_links( $args )
function like the example below to render pagination in archive template file.( 5 ) The
paginate_links()
function output theul li
listing withpage-numbers
class.If you use bootstrap inject
pagination
class to theul
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 🙂
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
Basically that’s
that holds the page number
from the working code:
Use query_posts instead of wp_query. It should work.
This is my Code.
When you create any CPT then you need to add this function.
It will manage/adjust/reassign your memory.
So please add this line your code will work.
Precaution.
I ‘m sure,your pagination will work and let me know if it will not work.
Thanking you.