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.

I have a country taxonomy with children taxonomies > state & city.

For a given entry for a restaurant in Nantucket, I want it to say "Nantucket, Massachusetts, United States (each linked to those taxonomy pages)", but the code I have gotten to work (I'm new to PHP) outputs:

Massachusetts, Nantucket, United States

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

How to I get this to order by the reverse hierarchical order of the taxonomy > City, State, Country?

Thanks, in advance, for any help!

share|improve this question
    
Possible duplicate of wordpress.stackexchange.com/questions/37285/… –  marfarma Sep 18 '12 at 15:46
add comment

2 Answers

To expand on my comment above, you would replace your code with something like this:

print_taxonomy_ranks( get_the_terms( $post->ID, 'post_tags' ) );

and in your function print_taxonomy_ranks you'd replace the echo statement with your preferred output.

function print_taxonomy_ranks( $terms ) {
// if terms is not array or its empty don't proceed
if ( ! is_array( $terms ) || empty( $terms ) ) {
    return false;
}

foreach ( $terms as $term ) {
    // if the term have a parent, set the child term as attribute in parent term
    if ( $term->parent != 0 )  {
        $terms[$term->parent]->child = $term;   
    } else {
        // record the parent term
        $parent = $term;
    }
}

echo "Order: $parent->name, Family: {$parent->child->name}, Sub-Family: {$parent->child->child->name}";
}

Extracted from answer: Custom taxonomy, get_the_terms, listing in order of parent > child

share|improve this answer
add comment
     <?php

            /**
             * Retrieve a post's terms as a list ordered by hierarchy.
             *
             * @param int $post_id Post ID.
             * @param string $taxonomy Taxonomy name.
             * @param string $term_divider Optional. Separate items using this.
             * @param string $reverse Optional. Reverse order of links in string.
             * @return string
             */
            class GetTheTermList {

                public function get_the_term_list($post_id, $taxonomy, $term_divider = '/', $reverse = false) {
                    $object_terms = wp_get_object_terms($post_id, $taxonomy);
                    $parents_assembled_array = array();
                    //***
                    if (!empty($object_terms)) {
                        foreach ($object_terms as $term) {
                            $parents_assembled_array[$term->parent][] = $term;
                        }
                    }
                    //***
                    $sorting_array = $this->sort_taxonomies_by_parents($parents_assembled_array);
                    $term_list = $this->get_the_term_list_links($taxonomy, $sorting_array);
                    if ($reverse) {
                        $term_list = array_reverse($term_list);
                    }
                    $result = implode($term_divider, $term_list);

                    return $result;
                }

                private function sort_taxonomies_by_parents($data, $parent_id = 0) {
                    if (isset($data[$parent_id])) {
                        if (!empty($data[$parent_id])) {
                            foreach ($data[$parent_id] as $key => $taxonomy_object) {
                                if (isset($data[$taxonomy_object->term_id])) {
                                    $data[$parent_id][$key]->childs = $this->sort_taxonomies_by_parents($data, $taxonomy_object->term_id);
                                }
                            }

                            return $data[$parent_id];
                        }
                    }

                    return array();
                }

                //only for taxonomies. returns array of term links
                private function get_the_term_list_links($taxonomy, $data, $result = array()) {
                    if (!empty($data)) {
                        foreach ($data as $term) {
                            $result[] = '<a rel="tag" href="' . get_term_link($term->slug, $taxonomy) . '">' . $term->name . '</a>';
                            if (!empty($term->childs)) {
                                //***
                                $res = $this->get_the_term_list_links($taxonomy, $term->childs, array());
                                if (!empty($res)) {
                                    //***
                                    foreach ($res as $val) {
                                        if (!is_array($val)) {
                                            $result[] = $val;
                                        }
                                    }
                                    //***
                                }
                                //***
                            }
                        }
                    }

                    return $result;
                }

            }
            ?>

    <?php
    //EXAMPLE OF USING

        $term_list_object = new GetTheTermList();
        $car_location = $term_list_object->get_the_term_list($post_id, 'carlocation', '-',TRUE);

        ?>

Read about it here: http://pluginus.net/get-the-term-list-ordered-by-hierarchy/

share|improve this answer
add comment

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.