2015-04-01 87 views
1

我正在尝试对以下数组进行排序,以用于在每个类别的鹅溪,困溪和fobr输出最新帖子的foreach循环中使用。排序数组的类别

我想排序这个数组的日期,但我有点困惑如何完成这一点。向WP_Query参数中添加多个类别并删除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(); 
+0

你想输出后** **每这些类别的阵列?或者你只是想输出最近发布的三个帖子,只要它们属于这三个类别中的哪一个,他们所处的类别都不重要? – rnevius 2015-04-01 13:48:02

+0

**每个**的最新帖子 – 2015-04-01 13:49:32

回答

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();