代码之家  ›  专栏  ›  技术社区  ›  Ash Loudon

以slug顺序返回自定义分类法

  •  0
  • Ash Loudon  · 技术社区  · 6 年前

    我有一个循环可以拉入自定义的分类术语,我想按slug排序,但是当我添加'orderby'命令时,我的循环被破坏了,没有返回任何内容。毫无疑问,我使用了错误的语法,但我看不到我的生活在哪里!

    <?php
    
    $loop = new WP_Query(
        array(
            'post_type'      => 'camp',
            'orderby'        => 'name',
            'order'          => 'ASC',
            'tax_query' => array(
                array(
                    'taxonomy' => 'types',
                    'field'    => 'slug',
                    'terms'    => $term->slug,
                ),
            )
        )
    );
    
    if ($loop->have_posts()) :
    while ($loop->have_posts()) : $loop->the_post();
    $terms = get_lowest_taxonomy_terms(get_the_terms( get_the_ID(), array(
        'taxonomy' => 'destinations',
        'orderby' => 'slug',
        'order' => 'ASC'                                
    )) );
    ?>
    

    任何帮助都将不胜感激:)

    增加了附加功能:

    get_lowest_taxonomy_terms( $terms ) {
        // if terms is not array or its empty don't proceed
        if ( ! is_array( $terms ) || empty( $terms ) ) {
            return false;
        }
    
        $filter = function($terms) use (&$filter) {
    
            $return_terms = array();
            $term_ids = array();
    
            foreach ($terms as $t){
                if( $t->parent == 0 ) {
                    $term_ids[] = $t->term_id;
                }
            }
    
            foreach ( $terms as $t ) {
                if( $t->parent == false || !in_array($t->parent,$term_ids) )  {
                    //remove this term
                }
                else{
                    $return_terms[] = $t;
                }
            }
    
            if( count($return_terms) ){
                return $filter($return_terms);
            }
            else {
                return $terms;
            }
    
        };
    
        return $filter($terms);
    }
    

    循环应查询分类法类型“destination”,其中该目标具有“type”的附加分类法。

    enter image description here

    结果按CSS排序为3列,但如您所见,没有按字母顺序排序。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Community CDub    5 年前

    get_the_terms

    获取术语(int | object$post,string$taxonomy)

    $post (int | object)(必选)Post ID或object。

    $taxonomy (string)(必选)分类名称。

    More details of get_the_terms

    获取条款 没有任何排序选项。但是,你可以用 usort 对返回的数组进行排序。

    $terms = get_lowest_taxonomy_terms(get_the_terms( get_the_ID(), 'destinations' ));
    

    这是一个如何使用 乌索特

    $terms = get_the_terms( get_the_ID(), 'destinations' );
    usort($terms,"so980_sort_terms_alphabetically");
    function so980_sort_terms_alphabetically($a,$b) {
        return $a['slug'] > $b['slug'];//using 'slug' as sorting order
    }
    

    so980_sort_terms_alphabetically 功能应该在你的主题中 functions.php

    $terms = get_the_terms( get_the_ID(), 'destinations' );
    usort($terms,"so980_sort_terms_alphabetically");
    $terms = get_lowest_taxonomy_terms($terms);
    

    更新

    我刚刚测试了它,它返回了一个致命的错误,因为我犯了一个错误。错误消息: Fatal error: Cannot use object of type WP_Term as array

    获取条款 返回一个对象。所以,使用 $a['slug']

    在这里,

    function so980_sort_terms_alphabetically($a,$b) {
        return $a->slug > $b->slug;//using 'slug' as sorting order
    }
    

    更新2

    由于您的输出代码在您的问题中不可用,我将假设您正在循环内打印。而不是这样做,我们将把返回的项保存在另一个数组中,并在循环外对其排序,这样我们就可以一次获得所有的项。

    下面是一个简单的例子,说明如何做到这一点:

    if ($loop->have_posts()) :
        while ($loop->have_posts()) : $loop->the_post();
            $pre_terms = get_lowest_taxonomy_terms(get_the_terms(get_the_ID(), 'product_tag'));
            if ( $pre_terms ) {
                foreach ( $pre_terms as $pre_term ) {
                    $terms[] = $pre_term;
                }
            }
        endwhile;
    endif;
    usort($terms,"so980_sort_terms_alphabetically");
    
    //a quick test to see if it's working or not
    foreach ( $terms as $term ) {
        echo $term->slug;
        echo '<br/>';
    }