如何按以下降序排列帖子$json_data->data[0]->total_count
价值观我想显示排名靠前的共享帖子,但我不能真正使用WP_Query
或query_posts
正确的方式。
require(\'../wp-blog-header.php\');
query_posts(\'&showposts=-1\');
while (have_posts()) : the_post();
if ( has_post_thumbnail() ) {
the_post_thumbnail( array(40,40) );
}
$url = get_the_permalink();
$json = file_get_contents( \'https://graph.facebook.com/fql?q=SELECT%20like_count,%20total_count,%20share_count,%20click_count,%20comment_count%20FROM%20link_stat%20WHERE%20url%20=%20%27\' . $url . \'%27\' );
$json_data = json_decode($json, false);
echo $json_data->data[0]->total_count;
endwhile;
上面的代码显示了帖子和总共享数,但不是按降序排列的,因为我还没有应用任何规则。
有什么想法吗?
提前谢谢!!
最合适的回答,由SO网友:Sladix 整理而成
您可以将计数存储为键,并链接一个值并在显示之前对其进行排序,如下所示:
$sort_tab = array();
while (have_posts()) : the_post();
if ( has_post_thumbnail() ) {
the_post_thumbnail( array(40,40) );
}
$url = get_the_permalink();
$json = file_get_contents( \'https://graph.facebook.com/fql?q=SELECT%20like_count,%20total_count,%20share_count,%20click_count,%20comment_count%20FROM%20link_stat%20WHERE%20url%20=%20%27\' . $url . \'%27\' );
$json_data = json_decode($json, false);
$sort_tab[$json_data->data[0]->total_count] = $url;
endwhile;
sort($sort_tab);
foreach($sort_tab as $count=>$link)
{
echo $link.\' has \'.$count.\' votes\';
}