您应该过滤默认值$query
通过连接到的回调pre_get_posts
:
function wpse108983_filter_pre_get_posts( $query ) {
// Make sure we\'re targeting
// the main loop query, and
// only in the archive context
if ( $query->is_main_query() && is_archive() ) {
// Add your query modifications here
// For example, to sort by date:
$query->set( \'orderby\', \'date\' );
}
}
add_action( \'pre_get_posts\', \'wpse108983_filter_pre_get_posts\' );
如果您需要输出多个循环以便按分类法分组,那么您将不使用上述方法,而是希望通过以下方式输出自定义循环
new WP_Query()
.
假设您已经知道如何查询分类的术语foobar
, 将它们排列成一个阵列,$term_array
:
// Loop through each term
foreach ( $term_array as $single_term ) {
// Generate the term query
$term_query = new WP_Query(
\'foobar\' => $single_term,
\'orderby\' => \'date\'
);
// Output the term query loop
if ( $term_query->have_posts() ) :
while ( $term_query->have_posts() ) :
$term_query->the_post();
// Custom loop markup goes here
// Use normal loop template tags, such as
// the_content(), the_permalink(), etc
endwhile;
endif;
// Important; don\'t forget this!
wp_reset_postdata();
}