2

enter image description hereI have created custom taxonomy called "product_category", it has been shared with post type "books" and "movies" .

So I need to create a different template for both post types.

Both the templates' design will be different, so its important that both the post_type shared taxonomy (even their terms will be shared in both the post_type.) should have different template .

Path should be like :-

site/books/product_category/horror/

site/movies/product_category/horror/

1

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.

  • hi your code work perfectly, but if any one try;s to open URL normaly like "site/product_category/horror/" its give a blank page. i tried to add taxonomy template but it doesn't work. – user2506619 Jul 2 '14 at 11:24
  • It should work if you've created taxonomy page. Refer: codex.wordpress.org/images/1/18/Template_Hierarchy.png – Abhineet Verma Jul 2 '14 at 13:05
  • the custom_term_link function breaks if there is no post available in the following term. and i need the link even if the term is empty. please Improve this function. – user2506619 Jul 18 '14 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.