• 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

    https://www.ads-software.com/support/topic/insert-category-of-taxonomy-between-custom-post-type-and-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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi Cortez77,

    This was my problem too. I spent lot of days with Google searches and reading- Finally I found the solution:

    https://wordpress.stackexchange.com/questions/96126/custom-post-type-taxonomy-slug-post-title-with-post-type-archive?answertab=votes#tab-top

    Use the code from Answer 1 and replace

    $newrules[‘our-work/(.+?)/page/?([0-9]{1,})/?$’] = ‘index.php?post_type=work&work_category=$matches[1]&paged=$matches[2]’;

    to

    $newrules[‘our-work/(.+?)/page/?([0-9]{1,})/?$’] = ‘index.php?work_category=$matches[1]&paged=$matches[2]’;

    (delete ‘post_type=work&’)

    It works fine for me.

    Thread Starter Cortez77

    (@cortez77)

    Thanks for taking the time to share zinifexer!
    I ended up setting up an ajax lazy load type deal to get around the problem myself.
    I’ll give this a try too though to support those without javascript.
    Cheers

    @zinifexer thanks, your code works perfect.

    @zinifexer thanks, your code works perfect.

    https://eisabainyo.net/weblog/2010/03/10/display-5-latest-posts-in-each-category-in-wordpress/
    I want to change but another post lists are

    Display 5 posts in each category in, problem.
    Please help obtain very pivotal for me

    <?php
    $cat_args = array(
    	'taxonomy' => 'work_category'
    
    );
    $categories =   get_categories($cat_args);
    foreach($categories as $category) {
        echo '<dl>';
        echo '<dt> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></dt>';
         $post_args = array(
          'numberposts' => 5,
           'post_type' => 'work',
          'taxonomy' => $category->term_id
        );
        $posts = get_posts($post_args);
    
    	foreach($posts as $post) {
    	?>
    		<dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
    	<?php
    	}
    	echo '<dd class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>View all posts in ' . $category->name.'</a></dd>';
    	echo '</dl>';
    }
    ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Adding Taxonomy Name to Custom Post Type and Retain Pagination’ is closed to new replies.