我正在尝试从每个类别/分类中获取最新帖子的列表。我已经找到了解决方案,但我希望整个列表sorted by post date. 目前,它是按类别/分类名称排序的。
因此,如果我在A类中发帖,然后在B类中发帖,最后在C类中发帖:发帖顺序为:C、B、A(最新的在前)。如果我随后在cat B中发布,则顺序应更改为:B、C、A
我真的希望它能与自定义分类法一起使用,但默认类别也能适用于我。
到目前为止,我掌握的代码来自此网站:http://wptheming.com/2012/08/display-the-most-recent-post-in-each-category/
//Retrieves all the terms from the taxonomy portfolio_category
//http://codex.wordpress.org/Function_Reference/get_categories
$cat_args = array(
\'type\' => \'post\',
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'taxonomy\' => \'tax_name\');
$categories = get_categories( $cat_args );
// Pulls the first post from each of the individual portfolio categories
foreach( $categories as $category ) {
$args = array(
\'posts_per_page\' => 1,
\'post_type\' => \'post\',
\'tax_name\' => $category->slug,
\'no_found_rows\' => true,
\'update_post_meta_cache\' => false,
\'update_post_term_cache\' => false
);
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div id="item">
<b><?php the_title(); ?></b>
<?php the_content(); ?>
</div>
<?php endwhile;
}
// Reset Post Data
wp_reset_postdata();
?>
编辑:我尝试编辑代码的第一部分以:
$cat_args = array(
\'type\' => \'post\',
\'orderby\' => \'date\',
\'order\' => \'DESC\',
\'taxonomy\' => \'tax_name\');
$categories = get_categories( $cat_args );
但它仍然按照分类名称的顺序显示帖子?
SO网友:sanchu
在代码中-查找
$cat_args = array(
\'type\' => \'post\',
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'taxonomy\' => \'tax_name\');
$categories = get_categories( $cat_args );
重新安装
$cat_args = array(
\'type\' => \'post\',
\'orderby\' => \'date\',
\'order\' => \'DESC\',
\'taxonomy\' => \'tax_name\');
$categories = get_categories( $cat_args );