Archive template for taxonomy terms

I have registered a custom post type [equipment] and have a taxonomy of [equipment_type] within the taxonomy I have parent and child categories. For example:

Equipment (Custom post type)

Read More

Equipment Types (Taxonomy)

Cameras (Parent term)

  • Camera A (Child term)

  • Camera B

What I would like to create is effectively an archive page for the taxonomy terms. So when either ‘Cameras’ or ‘Camera A’ is selected it shows say 12 posts with title and featured image (links to single post) plus some pagination.

I have tried a standard WP query and Loop and it always ends up showing all of the taxonomies posts in all terms.

I currently have a taxonomy-equipment_types.php template set up to handle the query.

Related posts

Leave a Reply

3 comments

  1. I want to document this because I just found the answer recently.

    The problem with having taxonomy is that most developers have the mindset of expecting the taxonomy to be seen inside the post_type url of:

    http://hostname/post_type/taxonomy_term
    

    Instead, you are going to find the url in:

    http://hostname/taxonomy_slug/taxonomy_term
    

    This means that we often may be creating the template correctly as

    taxonomy-taxonomy_slug-taxonomy_term.php
    

    But the right way of using it is to expect it inside the url

    http://hostname/taxonomy_slug/taxonomy_term
    

    To view the correct url for the taxonomy, we can use

    get_the_term_list($post->ID,'taxonomy_slug')
    

    And test wherever the link is going to point to.

  2. The WordPress Template Hierarchy provides the exact template file that you need: taxonomy-{taxonomy}-{term}.php.

    So, to create a custom template for the cameras term of the equipment_types taxonomy, you would create a file named taxonomy-equipment_types-cameras.php.

    (Note, you can also create a template file for the taxonomy itself; just omit the {term} slug: taxonomy-{taxonomy}.php, or taxonomy-equipment_types.php in your case.)

    You can conditionally output content based on hierarchy by querying for either the term parent, via the object properties returned by get_term(), or the term children, via get_term_children().

  3. I had a similar issue. The problem with the above answers is that they all require you to specify the CPT, the taxonomy or the term.

    If – as you’ve indicated – you want to retrieve this dynamically depending on which CPT page the user is on, you can try the following (which works for me), which displays all the taxonomies for the current custom post type.

    (originally from this post with the help of GhostToast)

     <?php get_header(); ?>
      <div id="content">
    <div id="main">
    
      <ul>
    
    <? // Start taxonomy term archives query
    $post_type = get_post_type(); // find out CPT name
    $taxonomies = get_object_taxonomies($post_type); // Find taxonomies
    if($taxonomies){
    foreach($taxonomies as $taxonomy){
    // only want hierarchial -- no tags please
    if(is_taxonomy_hierarchical($taxonomy)){
    
        $terms = get_terms($taxonomy, array(
    'orderby'       => 'name', 
    'order'         => 'Asc',
    'hide_empty'    => true )); 
    
     foreach ( $terms as $term ) {  
    
    
    // example output below ?>
    
    <li>
    
     <h1><a href="<?php echo get_term_link($term->slug, $taxonomy); ?> "><? echo $term->name; ?></a></h1>
    
       <div class="imgBox">
    
         <a href="<?php echo get_term_link($term->slug, $taxonomy); ?> " title="<? echo $term->name; ?>" >
         <img src="<?php echo get_template_directory_uri(); ?>/timthumb.php?src=/library/images/dingy-placeholder.png&amp;h=196&amp;w=285&amp;zc=1" alt="<?php the_title(); ?>" /></a>
       </div>
    
               <div class="the-excerpt">
                <a href="<?php echo get_term_link($term->slug, $taxonomy); ?> "><? echo $term->description; //you can add this in admin ?> - click to view more</a>
                </div>
     </li>
            <?  
    
       }
     }
    }
    }?>
    
         </ul>
    
    
    <?php wp_reset_query(); ?>
    
         </div> <!-- end #main -->
       </div> <!-- end #content -->
    
     <?php get_footer(); ?>
    

    Save the above in a file called something like archive-mycpt.php, then in archive.php add this after the header call;

    <?php get_header(); ?>
    
    <?php  // is this one of our CPT ? If so, direct to custom archive page 
      if ( is_custom_post_type() ){ 
    include (TEMPLATEPATH . '/archive-mycpt.php'); 
    
            // if not continue...
    
        } else {  ?>
    
                   <!-- archive.php content   -->
    
       <? } ?>