甚至更具体的循环定位
假设我们想要更改 主循环 ,仅针对特定分类法或帖子类型。
仅针对 book
帖子类型存档页面上的主循环。
add_action( 'pre_get_posts', 'my_callback_function' );
function my_callback_function( $query ) {
if( !$query->is_main_query() || is_admin() ) return;
if( !is_post_type_archive( 'book' ) ) return;
// this code will run only if
// - this query is main query
// - and this is not admin screen
// - and we are on 'book' post type archive page
}
你还可以使用 is_category()
,is_tag()
和 is_tax()
检查类别,标签或自定义分类存档页面。
你可以使用 WordPress 中提供的任何条件标记。