按预定顺序排序 get_categories() 结果

Ordering get_categories() result by predetermined order

我想知道为什么 get_categories() 是自动升序的。我想 return 一个数组,基于包含类别 ID 的顺序。

这是我的代码。

<?php

$args = array(
    'include'=> '5,4,7,6',
);
$cats = get_categories($args);

?>

这是 Wordpress 函数参考

wordpress function reference

基本上您需要将参数数组传递给 get_categories 函数

$args = array(
  'orderby' => 'name',
  'order' => 'ASC',
  'include' => '5,4,7,6'
 );

$categories = get_categories($args);

编辑:

$args = array(
'orderby' => 'ID',
'order' => 'DESC',
'include' => '5,4,7,6'
 );

$categories = get_categories($args);

我完全不确定您为什么要使用 get_categories(),因为您已经知道要查找的订单以及目标 ID。

相反,我会使用 get_category(),并使用简单的 foreach 循环生成 $categories 数组:

$categories = array();
$cat_ids = array(5,4,7,6);

// A simple foreach loop, to keep things in your required order
foreach ( $cat_ids as $id ) {
    $categories[] = get_category( $id );
}