A shared custom taxonomy between two custom post types

I have two custom post types: event and contact:



Both of them uses a custom taxonomy: country

I just created a template named: taxonomy-country.php

But I Need two separated templates, one for events who uses countries and another one for contacts who uses countries. For example, to display all events in United States or all contacts from Costa Rica.

This is my actual code of taxnomy-country.php, but only for events who uses countries.

 * The template for displaying Countries Taxonomy.


$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );


$args = array();

$args['post_type']  = 'event';
$args['taxonomy']   = $term->taxonomy;
$args['term']   = $term->slug;


        <div id="container">
            <div id="content" role="main">

                <h1 class="page-title"><?php
                    printf( __( 'Archives: %s', 'twentyten' ), '<span>' . $term->name . '</span>' );
                    $category_description = category_description();
                    if ( ! empty( $category_description ) )
                        echo '<div class="archive-meta">' . $category_description . '</div>';

                /* Run the loop for the category page to output the posts.
                 * If you want to overload this in a child theme then include a file
                 * called loop-category.php and that will be used instead.
                get_template_part( 'loop', 'category' );

            </div><!-- #content -->
        </div><!-- #container -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Thanks in advance.

Solutions Collecting From Web of "A shared custom taxonomy between two custom post types"

You use get_query_var(‘post_type’) to set the post type in your args array

$args['post_type']  = get_query_var('post_type');

and also include the right loop part based on that for example:

if (get_query_var('post_type') == "event"){
   get_template_part( 'loop', 'event' );
   get_template_part( 'loop', 'other' );

Have you already look into what comes back from $term-taxonomy?

Try this right before your query:

echo '<pre>';
echo '</pre>';

I don’t know exactly the output without trying it now, but i guess it’s an array and in this case you could just take the taxonomy you need.

Note: You don’t need to specify $args = array(). The conversion to an array already happens when you type $args[] = '';