自定義分類法創建
add_action( 'init', 'ashu_post_type' );
function ashu_post_type() {register_taxonomy('province','post',array('label' => '省','rewrite' => array( 'slug' => 'province' ),'hierarchical' => true));register_taxonomy('city','post',array('label' => '市','rewrite' => array( 'slug' => 'city' ),'hierarchical' => true));register_taxonomy('genre','post',array('label' => '類型','rewrite' => array( 'slug' => 'genre' ),'hierarchical' => true));
}
重寫規則和模板設置:在主題文件夾下建立page-sift.php
文件作為篩選頁面的模板文件,并添加重寫規則等。部分關鍵代碼如下
// 獲取篩選頁面的Url
function ashuwp_sift_link() {return home_url( "/sift" );
}// 添加query變量
function ashuwp_query_vars( $public_query_vars ) {$public_query_vars[] = 'ashuwp_page';$public_query_vars[] = 'condition';return $public_query_vars;
}// sift頁面的重寫規則
function ashuwp_rewrite_rules( $wp_rewrite ) {$new_rules = array(&