Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a hierarchical custom taxonomy ("photos"), with 3 parent terms

  • color
  • location
  • year

Each parent term has several children terms. I have associated the "photos" taxonomy with pages and have tagged a bunch of pages with the relevant terms for each page.

For example, one page has terms:

  • red (child of "color")
  • sf (child of "location")
  • 2010 (child of "year")

What I would like to do is have some sort of conditional tag that displays the terms as such:

  • Color: Red
  • Location: SF
  • Year: 2010

Where the terms are linked.

I have tried to set up a conditional has_term but am blanking on how to echo the term from the array:

<strong>Color:</strong> <?php if( has_term( array( 'red', 'blue', 'green' ), 'photos' )  ) {
    // do something here
}
?>

That code checks to see if any of the three terms ('red', 'blue', 'green') are associated with that page, and it works fine to test the term. I just do not know how to echo the active term.

I could always create taxonomies for each of what are now my parent level terms ("Color", "Location", and "Year"), but if there is another way to do it without separate taxonomies, that would be great.

Any suggestions would be very much appreciated.

share|improve this question

2 Answers 2

The following code is not tested, but should works:

In functions.php put

function page_photos_terms($post_id = 0) {
   echo get_color_location_year($post_id, 'photos'); 
}

function get_color_location_year($post_id = 0, $taxonomy = 'photos') {
  if ( ! $post_id ) return '';
  $color = null;
  $location = null;
  $year = null;
  $out = array();
  $out_str = '';
  $ancestors = get_terms($taxonomy, array('parent' => 0) );
  if ( empty($ancestors) || is_wp_error($ancestors) ) return '';
  foreach ($ancestors as $ancestor ) {
    if ( $ancestor->slug == 'color') { $color = $ancestor; }
    if ( $ancestor->slug == 'location') { $location = $ancestor; }
    if ( $ancestor->slug == 'year') { $year = $ancestor; }
  }
  $terms = get_the_terms( $post_id, $taxonomy );
  if ( empty($terms ) || is_wp_error($terms) ) return '';
  foreach ( $terms as $term ) {
    if ( $color && ($term->parent == $color->term_id) ) {
       $out['color'][] = $term->name;
    } elseif( $location && ($term->parent == $location->term_id) ) {
       $out['location'][] = $term->name;
    } elseif( $year && ($term->parent == $year->term_id ) ) {
       $out['year'][] = $term->name;
    }
  }
  foreach ( array('color', 'location', 'year') as $p ) {
    if ( ! empty($out[$p]) && is_object($$p) ) {
      $out_str .= sprintf('<li><strong>%s</strong>: ', $$p->name );
      $out_str .= implode(', ', $out[$p]) . '</li>';
    }
  }
  if ( $out_str != '' )  $out_str = '<ul class="photos-meta">' .  $out_str . '</ul>';
  return $out_str;
}

In page.php (or everywhere you need) put:

page_photos_terms( get_the_id() );

Function can be used with all post types that support photos taxonomy.

It works even outside the loop (passing a page/post id) and even if there are more than one color (or year, or location) on the page/post.

Hope it helps.

share|improve this answer

Suppose your Color term has ID = 10. Replace "10" with your desired ID

<?php
$termID = 10;
$taxonomyname = "photos";
$termchildren = get_term_children( $termID, $taxonomyname );  

if (is_array($termchildren)){
echo "Color: ";
foreach ($termchildren as $child) {
    $term = get_term_by( 'id', $child, $taxonomyname );
    if (has_term($term->ID, $taxonomyname)){
       echo '<a href="' . get_term_link( $term->name, $taxonomyname ) . '">' . $term->name . '</a>';
    }
}
?>

Rinse and repeat for the other top taxonomies.

share|improve this answer
    
Thank you. I tried that and it returned ALL children for that parent term. I am looking for only the term that is associated with that particular page. There has to be a way but I can't figure it out. –  Ethan Dec 14 '12 at 14:19
    
I have corrected the code to only display if has_term. Should work now –  K Themes Dec 15 '12 at 11:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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