按层次结构对Get_Terms()进行排序

时间:2019-01-08 作者:Abhik

我试图以保持层次结构的方式查询自定义分类术语。

这是我正在使用的代码。。

function ev_test_data() {
    $title = \'Mens Levi\\\'s Jeans\';
    $titles = str_replace( array("\'", \'"\', \'-\', \'\\\\\' ), \'\', explode( \' \', trim($title)) );
    $cats = array();
    foreach ( $titles as $kw ) {
        $terms = get_terms( array(
            \'taxonomy\' => \'product_cat\',
            \'orderby\' => \'parent\', // I guess this is where I am lost.
            \'hide_empty\' => false,
            \'name__like\' => $kw,
        ) );
        foreach ( $terms as $term ) {
            $cats[] = $term->term_id;
        }
    }

    $finals = array();
    foreach ( $cats as $cat ) {
        $catobj = get_term_by( \'id\', $cat, \'product_cat\' );
        $tree = \'\';
        $ancentors = array_reverse(get_ancestors( $cat, \'product_cat\', \'taxonomy\' ), true);
        $i=1;
        foreach ( $ancentors as $ancentor ) {
            $sterm = get_term_by( \'id\', $ancentor, \'product_cat\' );
            $tree .= $i == 1 ? \'<b>\' . $sterm->name . \'</b>\' : \' > \' . $sterm->name;
            $i++;
        }
        $tree .= \' > \' . $catobj->name;
        $finals[$cat] = $tree;
    }

    echo \'<pre>\' . print_r( $finals, true ) . \'</pre>\'; 
}  
这将生成此列表。

Array
(
    [4638] => Fashion > Womens Clothing
    [4699] => Fashion > Womens Handbags & Bags
    [4709] => Fashion > Womens Shoes
    [4461] => Fashion > Mens Accessories
    [4493] => Fashion > Mens Clothing
    [4512] => Fashion > Mens Shoes
    [4603] => Fashion > Womens Accessories
    [4779] => Fashion > Vintage > Mens Vintage Clothing
    [4821] => Fashion > Vintage > Womens Vintage Clothing
    [4290] => Fashion > Kids Clothing, Shoes & Accs > Boys Clothing (Sizes 4 & Up) > Jeans
    [4319] => Fashion > Kids Clothing, Shoes & Accs > Girls Clothing (Sizes 4 & Up) > Jeans
    [4354] => Fashion > Kids Clothing, Shoes & Accs > Unisex Clothing > Jeans
    [4500] => Fashion > Mens Clothing > Jeans
    [4660] => Fashion > Womens Clothing > Jeans
    [4666] => Fashion > Womens Clothing > Maternity > Jeans
)  
然而,这个列表并没有按我想要的顺序排列。我需要按层次顺序排序。像这样的。

Array
(
    [4493] => Fashion > Mens Clothing
    [4500] => Fashion > Mens Clothing > Jeans
    [4638] => Fashion > Womens Clothing
    [4660] => Fashion > Womens Clothing > Jeans
    [4666] => Fashion > Womens Clothing > Maternity > Jeans
    [4699] => Fashion > Womens Handbags & Bags
    [4709] => Fashion > Womens Shoes
    [4603] => Fashion > Womens Accessories
    [4512] => Fashion > Mens Shoes
    [4461] => Fashion > Mens Accessories
    [4779] => Fashion > Vintage > Mens Vintage Clothing
    [4821] => Fashion > Vintage > Womens Vintage Clothing
    [4290] => Fashion > Kids Clothing, Shoes & Accs > Boys Clothing (Sizes 4 & Up) > Jeans
    [4319] => Fashion > Kids Clothing, Shoes & Accs > Girls Clothing (Sizes 4 & Up) > Jeans
    [4354] => Fashion > Kids Clothing, Shoes & Accs > Unisex Clothing > Jeans

)
这可以使用WordPress内置函数来完成吗?如果不是,自定义查询的逻辑是什么?我并没有要求为我做这件事,但任何帮助或指导或headstart都会很好。

谢谢

2 个回复
SO网友:WizontheWeb

我也在寻找同样的东西,我认为这应该适合你:Custom taxonomy, get_the_terms, listing in order of parent > child

SO网友:RiddleMeThis

您可以使用wp_list_categories taxonomy参数,内置了对层次列表的支持。

相关推荐

GET_THE_TERMS与wp_GET_POST_TERMS中的奇怪结果

我正在尝试制作一个面包屑函数,但有一个小问题。。。使用时:$categories = get_the_terms( $post->ID, \'product_cat\' ); 我得到了一个循环中使用的类别数组,等等。唯一的问题是它是按字母顺序排列的。(我希望它按层次顺序排列。)经过一番挖掘,我发现了一种替代方法,即使用wp\\u get\\u post\\u terms(),但我肯定遗漏了一些东西,因为当我使用此方法时:$categories = wp_get_post_terms( $p