0

I have a question about how to echo a child taxonomy only.

My taxonomy is location.

I have a parent location and child locations like so:

London (parent)

  • West London (Child)

    • Chelsea (Grandchild)

I need to echo just the last term (in the case above, it's Chelsea).

At present the code I am using is:

$terms_as_text = get_the_term_list( $post->ID, 'location', '', ', ', '' ) ; echo strip_tags($terms_as_text);

Unfortunately, this echo's both parens/superiors as well.

How would i display just the last child term?

Many Thanks in advance

Paul

1
  • How does the return string look like?
    – Steven
    Commented Apr 23, 2012 at 10:42

1 Answer 1

1

Try something like this:

$terms_as_text = get_the_term_list( $post->ID, 'location', '', ', ', '' ) ; 
$terms_arr = explode('some-delimiter',$terms_as_text);
echo end($terms_arr); 
4
  • Hi Steven, Thanks for the response. Tried the above code, but unfortunately, this also shows all location taxonomies (parent, child, grandchild) etc.. and not the last term only. Any other ideas? thanks in advance.
    – PaulB
    Commented Apr 23, 2012 at 12:06
  • Remember to replace some-delimiter with your delimiter. That was why I was asking what the returned string looks like. Probably your delimiter is </a>
    – Steven
    Commented Apr 23, 2012 at 12:08
  • 1
    Hi Steven, by adding ', ' as the delimiter i can exclude the taxonomies : ) however I have noticed another problem : ( At present the taxonomies are being echoed in alphabetical order so it is impossible to display the last term only. Is there a way to use the above delimter but echo the taxonomies in reverse Hierarchical order? e.g grandchild, child, parent? Many Thanks
    – PaulB
    Commented Apr 23, 2012 at 12:41
  • I have to admit that taxonomies is not my strong side when it comes to WP. So all I can do is to point you to this page: codex.wordpress.org/Taxonomies
    – Steven
    Commented Apr 23, 2012 at 13:06

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.