Get the Term List - Ordering

时间:2012-09-18 作者:BBWood

我有一个国家分类法,其中儿童分类法>国家分类法;城市

对于Nantucket餐厅的给定条目,我希望它显示为“美国马萨诸塞州Nantucket(每个都链接到那些分类页面)”,但我使用的代码(我是PHP新手)输出:

马萨诸塞州,南塔基特,美国

$terms_as_text = get_the_term_list( $post->ID, \'country\', \'\', \', \', \'\' ) ; 
echo strip_tags($terms_as_text);
如何按照分类法>城市、州、国家的反向层次顺序对其进行排序?

有什么建议吗?

2 个回复
SO网友:marfarma

要扩展我上面的评论,可以将代码替换为以下内容:

print_taxonomy_ranks( get_the_terms( $post->ID, \'post_tags\' ) );
在你的功能中print_taxonomy_ranks 您可以用首选输出替换echo语句。

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}";
}
摘自this answer

SO网友:realmag777
     <?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);

        ?>
结束

相关推荐

Get Post Inside Get Terms问题

我有一个正确加载的get\\u术语在它里面,我用过get\\u帖子。然后使用get posts,我正在尝试获取每个post标记(count<;=当前不在此脚本中)<?php $categories = get_terms( \'blogs_cat\', array( \'type\' => \'blogs\', \'orderby\' => \'n