排序类别数组

时间:2015-04-01 13:35:25

标签: php wordpress wordpress-theming

我正在尝试对以下数组进行排序,以便在foreach循环中使用,该循环输出来自goose-creek,sleepy-creek和fobr的每个类别的最新帖子。

我想在发布日期之前对这个数组进行排序,但我对如何实现这一点感到困惑。将多个类别添加到WP_Query args并删除foreach循环会不会更好?

$feed_sources = array('goose-creek','sleepy-creek','fobr');


foreach ($feed_sources as $feed) {
$args = array('category_name' => $feed, 'posts_per_page' => 1);
$show = new WP_Query($args);
$show->the_post();

1 个答案:

答案 0 :(得分:0)

以下代码使用关联数组,并将类别作为键输出,将日期作为整数输出到值中。

Arsort()然后将数组从高到低排序

$feed_sources = array('goose-creek','sleepy-creek','fobr');
$dates_array = array();

foreach ($feed_sources as $feed) {
  $args = array('category_name' => $feed, 'posts_per_page' => 1);
  $show = new WP_Query($args);
  $show->the_post();
  $dates_array[$feed] = the_date('YmdHis','','',false);
}

arsort($dates_array);
$ordered_sources = array_keys($dates_array);

foreach ($ordered_sources as $feed) {
  $args = array('category_name' => $feed, 'posts_per_page' => 1);
  $show = new WP_Query($args);
  $show->the_post();