• Dear all,

    I have taxonomy named “location”. I want to show the taxonomy of my post with the children first to the parent of taxonomy

    Example:

    America (parent)
    – South America (child)
    – Argentina (sub child)
    – Buono aires ( sub sub child)

    i need to show like this
    Buonos aires, Argentina, South America, America

    Need your suggest

    Thank you

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Would your post have all those term assigned or just Buenos Aires? The single term is actually easier, sorting terms that may or not be ancestors can be tricky. Then when assigning terms, the author only needs to assign the one term and not try to determine all the proper ancestors. The following algorithm will do that automatically (and without error). If the assigned term is not already available on the template, get it with wp_get_post_terms(). An array is always returned, even if only one term. If only one term, the assigned term object will be $terms[0]. Thus its parent term ID will be $terms[0]->parent

    Use get_term_by() to get the parent term object (Argentina). Repeat the process of getting the parent ID from the object and using that to get the parent object. Repeat until the parent ID == 0, indicating a top level term (The Americas). Before starting this process, initiate an empty array to hold the term names. Each time you get a term object, push the name onto the array. When all terms have been pushed onto the array, use implode() to generate a comma delimited list.

    implode() is an easy way to deal with the potential extra trailing commas when building lists. It is certainly not the only way, but I like it ?? Just in case you really are using geographic terms and not just using them as an example, at least in English you should refer to the two American continents collectively as “The Americas”. People will confuse just “America” with the United States.

    Thread Starter gusswib

    (@gusswib)

    Hi,
    Thank you for response, i’m sorry for my english.

    I try this code

    $taxonomy = ‘location’;
    $terms = wp_get_post_terms( $id, $taxonomy, array( “fields” => “ids” ) );
    if( $terms ) {

    echo ‘

      ‘;
      $terms = trim( implode( ‘,’, (array) $terms ), ‘ ,’ );
      wp_list_categories( ‘title_li=&taxonomy=’ . $taxonomy . ‘&include=’ . $terms );
      echo ‘

    ‘;
    }

    that is show like this

    America (parent taxonomy) – South America (sub ) – Argentina (sub of sub) – Buones Aires

    i need to show like this

    Buones Aires – Argentina (sub of sub) – South America (sub ) – America (parent taxonomy)

    how could i do it ? need your help`

    Moderator bcworkz

    (@bcworkz)

    Your English is fine. I’m sorry if I sounded critical, that was not my intent. I know English has many quirks. I was hoping to clarify one of those quirks that no one would expect non-native speakers to know.

    So your posts do have all the terms assigned. It will be difficult to properly manage the order when wp_list_categories() is used. I know it’s useful to have formatted output done for you but in the end this function will not serve you well. Once you have the list sorted correctly, you should format the output with your own code.

    What needs to happen is the $terms array in your code needs to be sorted in descending order of the parent property of each object. This can be done using PHP’s usort() function. This means writing your own comparison function. Your function will be passed two term objects. It needs to compare the parent and term_id properties of each and return an appropriate value based on the result of that comparison.

    Since the two terms passed may not be immediately related, the comparison can get involved. If one term’s parent is the term_id of another, a result can be returned immediately. If not, the parent of one of the terms needs to be retrieved and that term’s parent compared against the other term’s ID. If no match, continue the same process until a match or parent == 0 is reached. When 0 is reached, we know the other term is not an ancestor, so it must be a descendant and a result can be returned.

    Because you want a descending list, the return value will be the reverse of what is normally expected. If that gets too confusing, you can return conventional ascending results, then reverse the array before output with array_reverse(). Your comparison function should allow for the possibility two terms have the same parent. This should not happen, but mistakes can happen when the terms are assigned.

    To avoid the need to continually make queries to get parent terms, your main function should create a lookup array of parent IDs keyed by the term’s ID. Declare this array global so that it can be accessed from within the comparison function. Many coders simply hate using globals. I generally agree, but the alternatives here are even worse.

    I hope you are able to make sense of all of that. I did warn in my initial reply that this would be tricky ?? You will find in the end the code is not so bad. Grasping the concept and getting it into code is the tricky part. If you need any clarification on any of this, just ask. I’ll do my best to explain.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Show Taxonomy With Children’ is closed to new replies.