如果不知道你的Instagram数据馈送对象是什么样子的,我会这样做,假设你已经有了$wp_query
对象中有帖子。
// we\'re going to merge both objects into one array so we can sort it
$merge_array = array();
// run through actual posts, assuming you already have a wp_query object built
while($loop->have_posts()): $loop->the_post();
$merge_array[] = array(
// store original post ID for later reference
\'post_id\' => $post->ID,
\'date\' => date(\'U\', strtotime($post->post_date)),
);
endwhile;
// now loop through instagram stuff and fill up merge array with it as well
foreach($mysterious_instgram_ojbect as $instagram_image){
$merge_array[] = array(
\'post_id\' => false, // set this false to know it is not WP post object
\'date\' => date(\'U\', $instagram_image->date), // guessing here
// store more info here such as image URL and/or title
);
}
现在,我们将重要的位填充到一个数组中。让我们把它们分类!
// sort them by date, function defined below
$merge_array_sorted = array_msort($merge_array, array(\'date\' => SORT_DESC));
并循环它们以获得最终输出。
foreach($merge_array_sorted as $feed_object){
echo date(\'F js\', $feed_object[\'date\']);
if($feed_object[\'post_id\']){
// is post, treat as wordpress post!
echo get_the_title($feed_object[\'post_id\']);
echo get_permalink($feed_object[\'post_id\']);
} else {
// is instagram, treat accordingly
}
}
把这个放在你的函数中。这是一个非常棒的多维数组分类器,如上所述。
// world famous multidimensional array sort function
function array_msort($array, $cols) {
$colarr = array();
foreach ($cols as $col => $order) {
$colarr[$col] = array();
foreach ($array as $k => $row) { $colarr[$col][\'_\'.$k] = strtolower($row[$col]); }
}
$eval = \'array_multisort(\';
foreach ($cols as $col => $order) {
$eval .= \'$colarr[\\\'\'.$col.\'\\\'],\'.$order.\',\';
}
$eval = substr($eval,0,-1).\');\';
eval($eval);
$ret = array();
foreach ($colarr as $col => $arr) {
foreach ($arr as $k => $v) {
$k = substr($k,1);
if (!isset($ret[$k])) $ret[$k] = $array[$k];
$ret[$k][$col] = $array[$k][$col];
}
}
return $ret;
}
这是一个松散的例子。对象现在应该按Unix时间戳排序,您可以将其转换回更有用的日期。您可以很容易地引用WP函数,如
the_content()
穿上你的最后一件衣服
foreach
环如果需要进一步解释,请告诉我。