不使用query_posts()
. 不要从模板中修改主循环查询。
您可以通过一个查询来提供所需内容(例如,通过筛选pre_get_posts
和groupby
), 但更可能的是,您需要使用WP_Query()
. 也许是这样的:
// Get all the months
$months = get_terms( $calendar );
// Set up the base query args
$events_query_args = array(
\'post_type\' => \'events\',
\'post_per_page\' => 10,
\'tax_query\' => array(
array(
\'taxonomy\' => \'calendar\',
\'field\' => \'slug\',
\'terms\' => \'\'
)
)
);
// Loop through the months, and
// output a custom query and loop
// for each
foreach ( $months as $month ) {
// Add $month to the query args
$events_query_args[\'tax_query\'][\'terms\'] = $month[\'slug\'];
// Build the query
$events_query = new WP_Query( $events_query_args );
// Open the query loop
if ( $events_query->have_posts() ) : while ( $events_query->have_posts() ) : $events_query->the_post();
// YOUR LOOP CODE GOES HERE
// Close the loop
endwhile; endif;
// Clean up
$events_query = null;
wp_reset_postdata()
}
当然,您必须添加实际的循环输出标记。