3

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)

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.

3
  • Please explain what you mean by Taxonomy Category. Your question conflates taxonomy and category (which is one type of taxonomy). Are you saying that you have hierarchical terms for your custom taxonomy, equipment_type? Also, is cameras a term for the equipment_type taxonomy? Dec 7, 2012 at 19:00
  • My apologies, perhaps I am confusing the terminology. I have a taxonomy of 'equipment_type' and within that I have 'cameras', 'lenses' etc... are those terms? They are Hierarchical and act like categories would on posts within the standard WP setup. Dec 7, 2012 at 19:07
  • Ah, now you have it! cameras, lenses, camera-a, and camera-b are terms of the equipment_type taxonomy. The camera-a and camera-b terms are children of the cameras term. Dec 7, 2012 at 19:45

3 Answers 3

4

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.

1

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

1
  • Thanks Chip, I thought the WP template provided what I needed as you suggested: taxonomy-equipment_types.php I think where I am struggling is working out how to output the content / posts based on the user selected terms for both parent and or child, cameras or cameras-a. Are suggesting I use 'get_term()' to decipher which content / posts should output in the loop? I'm fairly new to PHP and currently learning through Lynda.com. So I'm going to have to delve in a little more and try to understand the process. Could you provide a rough example to help me understand what I need to do? Dec 8, 2012 at 13:10
0

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

   <? } ?>

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.