这项工作有几个部分需要完成。
首先,我们注册分类法:
function wpa69163_register_txonomy() {
register_taxonomy(\'board\', \'post\', array(
\'labels\' => array(
\'name\' => _x( \'Boards\', \'taxonomy general name\' ),
),
\'rewrite\' => array(
\'slug\' => \'board\',
\'with_front\' => false
)
));
}
add_action( \'init\', \'wpa69163_register_txonomy\', 0 );
接下来,我们必须将过滤器添加到
term_link
要生成URL,请使用
term_id
对于我们的税务条款:
function wpa69163_term_link( $termlink, $term, $taxonomy ){
if( \'board\' == $taxonomy )
return home_url() . \'/board/\' . $term->term_id . \'/\';
return $termlink;
}
add_filter( \'term_link\', \'wpa69163_term_link\', 10, 3 );
最后,我们必须截取对税务条款的任何查询,并转换
board
将变量从ID查询回slug,以便WordPress能够加载正确的帖子:
function wpa69163_boards_query( $query ) {
if( isset( $query->query_vars[\'board\'] ) ):
if( $board = get_term_by( \'id\', $query->query_vars[\'board\'], \'board\' ) )
$query->query_vars[\'board\'] = $board->slug;
endif;
}
add_action( \'parse_query\', \'wpa69163_boards_query\' );