logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$8
Exclude taxonomy parent when outputting in the loop?

I have places in my template where I need to display the name of a country. Right now, I have a taxonomy of "Location" that lists continent names as the parent and country names as the children. I would like to be able to list only countries (without continent names) within the loop.

This is the code I've started with:

<?php echo get_the_term_list( $post->ID, 'Location', 'Country: ', ', ', '' ); ?>

Example of what above code gives me: Country: Africa, Nigeria
where I would like: Country: Nigeria

The other thing is that I would also like to be able to output the parent category name without the children in specific locations as well.

This question has been answered.

Jeremy Phillips | 02/28/11 at 8:33pm Edit


(3) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    02/28/11
    9:09pm
    Vidyut Kale says:

    <codel>

    <?php wp_list_categories('child_of=1&title_li=&taxonomy=location' ); ?>



    Where child_of=1 can be replaced by whichever continent's id you need.

    To output only the parent category names without children, try


    <?php wp_list_categories('depth=1&title_li=&taxonomy=location' ); ?>

  • avatar
    Last edited:
    03/01/11
    12:13am
    rilwis says:

    You can try this code:

    $terms = get_the_terms(get_the_ID(), 'location');
    $countries = array();
    foreach ($terms as $term) {
    if ($term->parent) $countries[] = $term->name;
    }
    echo 'Country: ' . implode(', ', $countries);

    • 02/28/11 11:05pm

      Jeremy Phillips says:

      Thank you, that worked very well! Just one more thing, how could I show just the continent (parent) name without the children (countries)?

    • 02/28/11 11:35pm

      rilwis says:

      Just change a bit in the code:

      $terms = get_the_terms(get_the_ID(), 'location');
      $continent = array();
      foreach ($terms as $term) {
      if (0 == $term->parent) $continent[] = $term->name;
      }
      echo 'Continient: ' . implode(', ', $continent);

  • avatar
    Last edited:
    03/01/11
    11:00am
    Nathan Epp says:

    to @rilwis 's code you can just add a ! infront of $term->parent

    if ( !$term->parent ) $countries[] = $term->name;

This question has expired.





Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.