如何按特定顺序对 get_terms() 结果进行排序

K8K*_*K8K 1 arrays sorting wordpress

我正在通过分配给变量的 get_terms() 的结果填充无序列表。我想做的是在无序列表中按特定顺序对结果进行排序。我该怎么做?我研究了 get_terms() 函数的 orderby 参数,但似乎没有一个参数能按照我需要的方式工作。如何定义它们的显示顺序?所以基本上我的无序列表在填充后看起来像这样:

  • 田径运动
  • 社区
  • 公司的
  • 卫生保健
  • 高等教育
  • k-12
我希望它看起来像这样:
  • k-12
  • 高等教育
  • 卫生保健
  • 田径运动
  • 公司的
  • 社区

Pie*_*sen 5

最好的选择是编写一个包装函数get_terms,然后使用它usort()按您想要的顺序对术语进行排序。

这是代码,我对代码进行了注释,以便于理解和遵循(注意:此代码需要 PHP 5.4+

function get_terms_ordered( $taxonomy = '', $args = [], $term_order = '', $sort_by = 'slug' )
{
    // Check if we have a taxonomy set and if the taxonomy is valid. Return false on failure
    if ( !$taxonomy )
        return false;

    if ( !taxonomy_exists( $taxonomy ) )
        return false;

    // Get our terms    
    $terms = get_terms( $taxonomy, $args ); 

    // Check if we have terms to display. If not, return false
    if ( empty( $terms ) || is_wp_error( $terms ) )
        return false;

    /** 
     * We have made it to here, lets continue to output our terms
     * Lets first check if we have a custom sort order. If not, return our
     * object of terms as is
     */
    if ( !$term_order )
        return $terms;

    // Check if $term_order is an array, if not, convert the string to an array
    if ( !is_array( $term_order ) ) {
        // Remove white spaces before and after the comma and convert string to an array
        $no_whitespaces = preg_replace( '/\s*,\s*/', ',', filter_var( $term_order, FILTER_SANITIZE_STRING ) );
        $term_order = explode( ',', $no_whitespaces );
    }

    // Remove the set of terms from the $terms array so we can move them to the front in our custom order
 $array_a = [];
 $array_b = [];
    foreach ( $terms as $term ) {
        if ( in_array( $term->$sort_by, $term_order ) ) {
            $array_a[] = $term;
        } else {
            $array_b[] = $term;
        }
    }

    /**
     * If we have a custom term order, lets sort our array of terms
     * $term_order can be a comma separated string of slugs or names or an array
     */
    usort( $array_a, function ( $a, $b ) use( $term_order, $sort_by )
    {
        // Flip the array
        $term_order = array_flip( $term_order );

        return $term_order[$a->$sort_by] - $term_order[$b->$sort_by];
    });

    return array_merge( $array_a, $array_b );
}   
Run Code Online (Sandbox Code Playgroud)

该函数有四个参数

  • $taxonomy这是从中获取术语的分类法。默认值:空字符串

  • $args这是可以传递给 的所有有效参数get_terms。您可以检查get_terms有效的参数。默认值:空数组

  • $term_order术语的别名或名称或 ID 按照您希望的特定顺序排序。这可以是 slugs/names/ids 数组或逗号分隔的 slugs/names/ids 字符串。默认空字符串

    例子

    string: $term_order = 'term-3, term-1, term-2';
    array:  $term_order = ['term-3', 'term-1', 'term-2'];
    
    Run Code Online (Sandbox Code Playgroud)

    这将按以下顺序显示术语term-3, term-1, term-2

  • $sort_by术语必须按哪个字段排序。默认值是 slugs,因此这意味着您应该将字符串或术语 slugs 数组传递给参数$term_order。如果需要将术语名称传递给$term_order参数,则需要将 的值设置$sort_byname。如果您愿意,您还可以将术语 id 传递给$term_order,在这种情况下,您需要将该$sort_by值设置为term_id

用法

在您的模板中,您将使用以下示例中的函数

分类名称是category,我们不想设置任何特定参数,我们需要按以下顺序按名称对术语进行排序Term C, Term A, Term B

然后,您将执行以下操作:($term_order作为数组

$terms = get_terms_ordered( 'category', [], ['Term C', 'Term A', 'Term B'], 'name');
var_dump( $terms);
Run Code Online (Sandbox Code Playgroud)

$term_order作为字符串

$terms = get_terms_ordered( 'category', [], 'Term C, Term A, Term B', 'name');
var_dump( $terms);
Run Code Online (Sandbox Code Playgroud)