首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按层次排序get_terms()

按层次排序get_terms()
EN

WordPress Development用户
提问于 2019-01-08 16:33:53
回答 2查看 3.8K关注 0票数 0

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

这是我正在使用的代码。

代码语言:javascript
复制
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 ? '' . $sterm->name . '' : ' > ' . $sterm->name;
            $i++;
        }
        $tree .= ' > ' . $catobj->name;
        $finals[$cat] = $tree;
    }

    echo '' . print_r( $finals, true ) . ''; 
}  

这就产生了这个列表。

代码语言:javascript
复制
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
)  

但是,这个列表不是按我的要求排序的。我需要按等级顺序排序。就像这样。

代码语言:javascript
复制
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来完成吗?如果不是,自定义查询的逻辑是什么?我并不是要求为我做这件事,但是任何帮助、指导或先发制人都将是很棒的。

谢谢

EN

回答 2

WordPress Development用户

发布于 2019-02-21 09:00:36

我在寻找同样的东西,并认为这应该适用于你:自定义分类法_这个_术语,按父母>子女的顺序排列

票数 2
EN

WordPress Development用户

发布于 2019-01-08 16:42:39

您可以使用支持层次结构列表的wp_列表_类别分类法参数。

票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/325013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档