php - Wordpress 类别帖子的奇怪顺序
问题描述
有没有人见过这个?
当我们在这里访问 wordpress 类别帖子之一时:
在最上面的项目上,它不是最新发布的文章,而是最旧的文章。
php代码的哪一部分应该改变?此链接适用于 4.5 的 wordpress 版本。
解决方案
function change_category_order( $query ) {
$category = get_queried_object();
$cat_id=$category->term_id;
if ( $query->is_category($cat_id) && $query->is_main_query() ) {
$query->set( 'order', 'DESC' );
}
}
add_action( 'pre_get_posts', 'change_category_order' );
如果您使用任何自定义查询,请在后循环 'order' => 'DESC' 中添加它
$args = array(
'post_type' => 'post',
'order' => 'DESC', );
$q = new WP_Query($args);
或将其粘贴到 function.php
add_action( 'pre_get_posts', 'my_change_sort_order');
function my_change_sort_order($query){
if(is_archive()):
//If you wanted it for the archive of a custom post type use: is_post_type_archive( $post_type )
//Set the order ASC or DESC
$query->set( 'order', 'DESC' );
//Set the orderby
//$query->set( 'orderby', 'title' );
endif;
};