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.

The following code will display the child taxonomy term of the parent only.

$terms = get_the_terms($wp_query->post->ID, 'propertytype');
 $props = array();
 foreach ($terms as $term) {
 if ($term->parent)
 $props[] = $term->name;
 }
 echo implode(', ', $props);

For example: London (Parent), West London (Child) - the above will only echo West London (Child).

However, if there are multiple child levels e.g London (Parent), West London (Child), Chelsea (Grand Child)

This does not work! I only want to display the last term.

How would i modify the above to only display the last term even if there is more than one sub level?

Thanks

Paul

FINAL CODE USED

   $terms = get_the_terms($wp_query->post->ID, 'propertytype');
      //die(print_r($terms));
      $props = array();
      foreach ($terms as $term) { 
      $hasChildrenTest = get_term_children($term->ID, 'propertytype');
          if ($term->parent) {
              if (empty($hasChildrenTest) && !is_wp_error($hasChildrenTest)) {
              $props[] = $term->name;
              }
          }
      }
      echo $props[0];
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

You can check if the resulting term has any children before adding it to the array:

$terms = get_the_terms($wp_query->post->ID, 'propertytype');
 $props = array();
 foreach ($terms as $term) {
    $hasChildrenTest = get_term_children($term->ID, 'propertyType');
 if ($term->parent) { 
    if (empty($hasChildrenTest) && !is_wp_error($hasChildrenTest)) {
         $props[] = $term->name;
    }
 }
 echo implode(', ', $props);

ps: the if($term->parent) test might become unnecessary after this. Check if you really need it.

edit: my mistake -> it should be empty $hasChildrenTest instead of !empty $hasChildrenTest so if the term has children it doesn't get added.

share|improve this answer
    
Hi Moraleida, Thanks for the response. Unfortunately i am getting - Parse error: syntax error, unexpected T_VARIABLE, expecting '(' for line $props[] = $term->name; any ideas? Thanks in advance –  PaulB May 14 '12 at 21:29
    
my code was missing a pair of parethesis on the empty check, sorry about that. I've updated the example. –  moraleida May 14 '12 at 21:43
    
Hi Again, thanks for the response, appreciate it! For some reason when i try the code, it produces another Parse error: syntax error, unexpected T_ENDWHILE for a later line in the file (<?php endwhile; else: ?>) Not sure why? –  PaulB May 15 '12 at 9:10
    
That error means there's something missing before the endwhile; statement. Check if you have every parenthesis, brackets and semicolons in place. Also consider moving your code to an IDE (like NetBeans or Eclipse) or a better text editor (such as Notepad++). That will help you find out where your syntax errors are. –  moraleida May 15 '12 at 13:00
    
Hi Moreleida, Modified the code slightly to get it to work. <code>$terms = get_the_terms($wp_query->post->ID, 'propertytype'); //die(print_r($terms)); $props = array(); foreach ($terms as $term) { $hasChildrenTest = get_term_children($term->ID, 'propertytype'); if ($term->parent) { if (empty($hasChildrenTest) && !is_wp_error($hasChildrenTest)) { $props[] = $term->name; } } } echo $props[0];</code>. This seems to work : ) Thanks again –  PaulB May 15 '12 at 20:53
show 2 more comments

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.