甚至更具體的迴圈定位
假設我們想要更改 主迴圈 ,僅針對特定分類法或帖子型別。
僅針對 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 中提供的任何條件標記。