Adding Taxonomy Name to Custom Post Type and Retain Pagination
-
Hard to be brief and descriptive with a title like that lol.
Also hard to research as well so hoping someone can point me in the right direction.This is basically a continuation of this post
and Robert3d’s question on how to retain pagination while using lopinsjk’s filter.
So my situation is this.
I have a post type called “products”
I have a taxonomy called “product_category”
I want them all to be children of permalink “shop”The args for my CPT products
$args = array( "labels" => $labels, "public" => true, "has_archive" => true, "publicly_queryable" => true, "query_var" => true, "rewrite" => array("slug" => "shop/%product_category%","with_front" => false, "pages" => true ), "hierarchical" => false, "supports" => array("title","editor","thumbnail"), "exclude_from_search" => false); register_post_type( "products" , $args );
The args for my Custom Taxonomy Products
$args = array( 'labels' => $labels, 'hierarchical' => true, 'public' => true, 'query_var' => 'product_category', 'rewrite' => array('slug' => 'shop' ), '_builtin' => false, 'show_ui' => true, 'show_admin_column' => true, ); register_taxonomy( 'product_category', 'products', $args );
Then making it all tick using lopinsjk’s filter
add_filter('post_link', 'product_category_permalink', 1, 3); add_filter('post_type_link', 'product_category_permalink', 1, 3); function product_category_permalink($permalink, $post_id, $leavename) { if (strpos($permalink, '%product_category%') === FALSE) return $permalink; $post = get_post($post_id); if (!$post) return $permalink; $terms = wp_get_object_terms($post->ID, 'product_category'); if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug; else $taxonomy_slug = 'no-brand'; return str_replace('%product_category%', $taxonomy_slug, $permalink); }
So this works perfectly allowing me to have a page called “shop” where I display all the categories, and the taxonomy names are added correctly when viewing a single product.
The problem lies on the custom taxonomy page I created “taxonomy-product_category.php”. It works as intended when visiting:
shop/%product_category%
but not
shop/%product_category%/page/2/I’m just not sure where I should be trying to rewrite the pagination links and how honestly.
Should this be done using an add_rewrite_rule or something that should be added to lopinsjk’s filter?
I’ve attempted some rewrites with no luck.
Any assistance would be greatly appreciated.
- The topic ‘Adding Taxonomy Name to Custom Post Type and Retain Pagination’ is closed to new replies.