我最近在几个文字夏令营中谈到了这一点。Here\'s my talk from WC Portland, 和the slides, source code, etc. 具体查看配方#2。
首先,您可能应该有一个静态前缀来启动URL。这不是百分之百必要的,但如果没有它,您的重写将与页面永久链接冲突。如果您决定使用静态前缀,您可能希望将其设置为post类型,因为这是您的分类法组织的内容。我不知道那是什么,所以我猜测这是关于“导游”的在这种情况下,您的URI将类似于/guides/usa/diving/。
这里有一些代码可以帮助您开始。如果有什么不明白的地方,你应该看演示文稿,并跟着幻灯片一起看。
<?php
/**
* Setup custom permalinks with multiple taxonomies
*/
if ( !class_exists( \'WPSE_110508\' ) ) :
class WPSE_110508 {
private static $instance;
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new WPSE_110508;
self::$instance->setup();
}
return self::$instance;
}
public function setup() {
add_action( \'init\', array( $this, \'structures\' ) );
add_filter( \'post_type_link\', array( $this, \'post_type_link\' ), 10, 2 );
add_filter( \'term_link\', array( $this, \'term_link\' ), 10, 3 );
}
/**
* Register our data structures
*
* @return void
*/
function structures() {
register_taxonomy( \'location\', \'guide\', array(
\'label\' => \'Body Types\',
\'rewrite\' => array( \'slug\' => \'guides\' )
) );
add_permastruct( "all_activities", "guides/all/%activity%" );
register_taxonomy( \'activity\', \'guide\', array(
\'label\' => \'Activities\',
\'rewrite\' => array( \'slug\' => \'guides/%location%\' )
) );
register_post_type( \'guide\', array(
\'public\' => true,
\'label\' => \'Guides\',
\'rewrite\' => array( \'slug\' => \'guides/%location%/%activity%\' )
) );
}
/**
* Filter post type links for guides to replace %location% & %activity% if present
*
* @param string $link
* @param object $post
* @return string
*/
function post_type_link( $link, $post ) {
if ( \'guide\' == $post->post_type ) {
if ( $locations = get_the_terms( $post->ID, \'location\' ) ) {
$link = str_replace( \'%location%\', array_pop( $locations )->slug, $link );
}
if ( $activities = get_the_terms( $post->ID, \'activity\' ) ) {
$link = str_replace( \'%activity%\', array_pop( $activities )->slug, $link );
}
}
return $link;
}
/**
* Filter term links for activities to replace %location% with "all"
*
* @param string $termlink
* @param object $term
* @param string $taxonomy
* @return string
*/
function term_link( $termlink, $term, $taxonomy ) {
if ( \'activity\' == $taxonomy ) {
return str_replace( \'%location%\', \'all\', $termlink );
}
return $termlink;
}
}
function WPSE_110508() {
return WPSE_110508::instance();
}
add_action( \'after_setup_theme\', \'WPSE_110508\' );
endif;