Create separate template for shared custom taxonomy with shared terms

See, It’s quite difficult to have path like

site/books/product_category/horror/

site/movies/product_category/horror/

As wordpress functionality will conflict between custom_post_type & their taxonomy if try to keep url like you mentioned. I suggest you consider the I mentioned below as a solution of your problem.

site/product_category/horror/?post_type=books

site/product_category/horror/?post_type=movies

Now to keep template as per shared terms, Create two templates in your theme directory.

  • common-term-books.php
  • common-term-movies.php

And, use this code to redirect to template on the basis of custom post type:

add_filter( 'template_include', 'wpse_152146_template_override', 99 );
function wpse_152146_template_override(){
    if (is_tax()) {
        if ( 'books' == $_GET['post_type']) {

            $new_template = locate_template( array( 'common-term-books.php' ) );
            if ( '' != $new_template ) {
                return $new_template ;
            }

        }
        elseif ( 'movies' == $_GET['post_type']) {

            $new_template = locate_template( array( 'common-term-movies.php' ) );
            if ( '' != $new_template ) {
                return $new_template ;
            }

        }
        else
            return $template;
    }
    else
        return $template;
}

Additionally, You can also use custom_term_link function i created to get url on the basis of post_type

function get_custom_term_link( $term, $taxonomy, $post_type ){
    $link = get_term_link( $term, $taxonomy );
    return $link.'?post_type=".$post_type;
}

get_custom_term_link( "horror', 'product_category', 'books');

Don’t forget to create template file common-term-books.php & common-term-movies.php. Otherwise, it’ll just show blank white screen.

Leave a Comment