How to have two different versions of a tag/category/taxonomy archive page?

I have a site that has two different bodies of content: more formal content (using various different custom post types) and a blog (using a blog post type). These two different bodies of content use the same taxonomies—but I would like to have two different versions of the taxonomy archives: one for the formal content, and one for blog content.

For example, for a term science in the taxonomy subjects I’d like to have both

Read More
  • mysite.com/subjects/science AND
  • mysite.com/blog/subjects/science (or mysite.com/subjects/science/blog)

What’s the best way to accomplish this in WordPress? I don’t want to use pages, because terms are being added all the time. I just want some way to be able to provide two archive templates for taxonomy terms.

Related posts

Leave a Reply

2 comments

  1. My solution at the moment is to use a GET variable to request a different page template:

    • Regular version of taxonomy page: mysite.com/subjects/science
    • Blog version of taxonomy page: mysite.com/subjects/science?view=blog

    To handle the ?view=blog variable, I add this conditional to the top of taxonomy.php (or taxonomy-subjects.php and any other taxonomy-{slug} that I want to have an alternate version of):

     /****************************************************
       Tell WordPress to fetch a different 
       template if our URL ends in ?view=blog
     ****************************************************/
    
    $view = $_GET["view"]; 
    
    if ( $view == "blog" ) {
        get_template_part( 'blog-archive' );
        exit();
    }
    

    Then, in the blog-archive.php template, I adjust the query_vars of $wp_query to output a loop with only blog posts.

    // adjust $wp_query->query_vars to taste
    $newloop = array_merge( $wp_query->query_vars, array( 'post_type' => 'blog', 'posts_per_page' => 5 )); 
    
    $the_query = new WP_Query ($newloop);
    
    while ( $the_query->have_posts() ) : $the_query->the_post();
    
        //output your posts however you like
    
    endwhile;
    wp_reset_postdata();
    

    This works for me at the moment; the only unfortunate aspect is that the URLs show ?view=blog instead of nice /blog. I borrowed the main $_GET[] idea from the answer here: How can I dynamically load another page template to provide an alternate layout of the posts?. (I wonder if the latter can be done more intuitively with $the_query = $wp_query->set(‘post_type’, ‘blog’); but this seemed to turn $the_query into a non-object and give an error.)

  2. The easy way, though not the only one, is to just use the default hierarchy for archives.php
    http://codex.wordpress.org/Template_Hierarchy

    So for you blog archives, use archives.php or a taxonomy based template like taxonomy-subject.php.
    Whatever the case you will need to alter the loop query to only show the posts for the term “science” in the taxonomy “subjects” that are posted in the default blog (post).

    Then for your custom post type archive you can create a template called archive-{post_type}.php (post type is the name you registered your CPT with). You can basically do the same custom query as above but only include posts in the custom post type ( and none from posts aka your blog).

    This is what you need to get familiar with for custom query’s, http://codex.wordpress.org/Class_Reference/WP_Query#Type_.26_Status_Parameters