下面是一些可以实现此目的的代码:
//Creates special permastruct that turns all terms of cities into slugs
add_action( \'init\', \'set_rewrite\' );
function set_rewrite() {
//Get terms
$terms = get_terms( \'cities\', array( \'hide_empty\' => false ) );
//return if no terms
if( count( $terms ) < 1 ) return;
$slugs = wp_list_pluck( $terms, \'slug\' );
//create term regex
$termregex = \'(\' . implode( \'|\', $slugs) . \')\';
//New query var
add_rewrite_tag( \'%cityname%\', $termregex );
//Add permastruct
add_permastruct( \'cities_struct\', \'%cityname%/\' );
//refresh if new term added
if( get_option( \'update_cities_struct\' ) ) {
global $wp_rewrite;
$wp_rewrite -> flush_rules();
update_option( \'update_cities_struct\', 0 );
}
}
//action to refresh rewrite rules when a new term is saved
add_action( \'created_cities\', \'refresh_cities\' );
add_action( \'edited_cities\', \'refresh_cities\' );
add_action( \'delete_cities\', \'refresh_cities\' );
function refresh_cities() {
update_option( \'update_cities_struct\', 1 );
}
//set city name to the taxonomy cities
add_filter( \'parse_query\', \'parse_cities_query\' );
function parse_cities_query( $query ) {
if( !empty( $query->query_vars[\'cityname\'] ) ) $query->set( \'cities\', $query->query_vars[\'cityname\'] )
}
基本上,这创建了一个特殊的permastruct,它使用分类法的slug
cities
作为其基础。它创建一个名为
cityname
, 它只接受分类法的slug,然后将结果发送到适当的分类法查询变量。