0

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];
0

1 Answer 1

0

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.

7
  • 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
    Commented May 14, 2012 at 21:29
  • my code was missing a pair of parethesis on the empty check, sorry about that. I've updated the example.
    – moraleida
    Commented May 14, 2012 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
    Commented May 15, 2012 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
    Commented May 15, 2012 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
    Commented May 15, 2012 at 20:53

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.