1

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

  • 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.

2 Answers 2

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.)

0

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

4
  • I know I can create post type archives and custom taxonomy archives, but I'm not sure I understand how archive-{post_type}.php would solve this problem. I need something like archive-{post_type}-{taxonomy}.php. I understand how to exclude blog entries from the standard taxonomy-subject.php template, which would get me halfway there; but archive-blog.php would just show all blog entries, while I want archives for every taxonomy terms with the functionality implied by archive-blog-subject.php.
    – supertrue
    Aug 12, 2011 at 21:58
  • You separate them in 2 files to make the custom query easier to manage/learn, you don't have to though, you can have 1 template serve multiple query's it's just more advanced. As the post said you need to read up on WP Query , so your basically telling the database to only output exactly what you want with it.
    – Wyck
    Aug 12, 2011 at 23:02
  • How would the URLs work?
    – supertrue
    Aug 12, 2011 at 23:20
  • However you want based on your permalink structure or rewrite, does that even matter? If so maybe accomplish the custom query's first then move on to url structure in another question.
    – Wyck
    Aug 12, 2011 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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