1

I am working on a project that has

  • a custom post type called 'Experience Statement'
  • Custom Taxonomy attached to that post type called 'Expertise'
  • 6 different terms for the custom taxonomy: Coastal, inland, marine, waterway, ship, telecommunications

So, I have created this loop, that works great and I have created 6 different template pages with this loop in each (I just change the term for each) <?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("post_type=experiencestatements&expertise=coastal&showposts=10&paged=$page"); if ( have_posts() ) : while ( have_posts() ) : the_post() ?>

So, this is working fine, but I have to create a different template page: i.e. page-exp-stmt-coastal.php for each term. I would like to have only one template page with conditional statements for each term instead of 6 (and counting) template pages. Having 6+ different pages means I have to go in and change each one every time the client wants a tweak.

In summary, I need some help crafting a conditional tag that changes the loop depending on the taxonomy term. AND, I have no idea how to do that. Any help is greatly appreciated.

Thanks, Larry

2 Answers 2

1

There are several ways to do this, one method is to use get_the_terms.

The logic would be to run the loop and customize the output based on the terms for that taxonomy ( this will probably not work out of the box).

//do a custom query here if needed
 if ( have_posts() ) : while ( have_posts() ) : the_post();

 $terms = get_the_terms($post->ID, 'Expertise');
//this will return and array of terms for your Expertise taxonomy.

   foreach ( $terms as $term ) {

     if($term->name == 'marine') {

       // do custom stuff here 

     }elseif($term->name == 'waterway') {

       // do custom stuff here
     }elseif .... 

After you comment it seems like you want no custom content per term, aka the same content for all 6 terms , this can be done with just a query loop, again there are several ways to do this.
http://codex.wordpress.org/Class_Reference/WP_Query

$args = array(
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'Expertise',
            'field' => 'slug',
            'terms' => array( 'marine', 'waterway', 'you_other_terms_here' ),
        ),

    )
)
$query = new WP_Query( $args );

Another way is to use my original suggestion and just do a match for all get_the_terms in the array you want using in_array , it could be something like ( continued from original code above), but there are several way to do this in php:

foreach ( $terms as $term ) {

if (in_array('marine', '$term')) && (in_array('waterway', '$term')) && etc..
   //do stuff
}
2
  • Thanks Wyck, This would allow me to have all the content on one page. This will be useful. But, here is more clarification of what I want. I want to use the same content, (..do custom stuff here ..) for each term. Is there a way to create the one template page that I apply to these 6 pages, then depending on which page it is, maybe page id, the loop would change??? In other words, I would rather not copy and paste the 'do custom stuff here' repetitively because this part is quite extensive, lots of custom field output, etc. Commented Jul 1, 2011 at 13:04
  • It is unclear why you were using 6 pages when it seems like you just want a basic loop for those terms with no custom content at all. I will adjust the answer above.
    – Wyck
    Commented Jul 1, 2011 at 17:31
1

couldn't you create a template called

taxonomy-Expertise.php

as per: http://codex.wordpress.org/Template_Hierarchy#Custom_Taxonomies_display

seems like you shouldn't have to adjust the loop at all as WP will already be using the appropriate query for each term in the taxonomy. but you could still test for the term name here (it'd be in the query vars) and trigger different display code as warranted.

Your Answer

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

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