下面是一个完整的示例,可以使用add_rewrite_rule()
. 首先记录本示例的基本设置,然后我们将使用add_rewrite_rule()
.
分类和后期类型注册genre
分类学和book
, movie
, 和game
post类型(请注意,本示例中使用了这些名称的单数版本,因为它被认为是best practice)。
// Create taxonomy: genre for post types: book, movie, and game
// https://codex.wordpress.org/Function_Reference/register_taxonomy
add_action( \'init\', \'wpse247013_register_taxonomies\', 0 );
function wpse247013_register_taxonomies() {
$args = [
\'public\' => true,
\'hierarchical\' => false,
\'label\' => __( \'Genres\', \'textdomain\' ),
\'show_ui\' => true,
\'show_admin_column\' => true,
\'query_var\' => \'genre\',
\'rewrite\' => [ \'slug\' => \'genres\' ],
];
register_taxonomy( \'genre\', [ \'book\', \'movie\', \'game\' ], $args );
}
// Create post types: movie, book, and game
// https://developer.wordpress.org/reference/functions/register_post_type/
add_action( \'init\', \'wpse247013_register_post_types\' );
function wpse247013_register_post_types() {
$book_args = [
\'label\' => __( \'Books\', \'textdomain\' ),
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => [ \'slug\' => \'books\' ],
\'capability_type\' => \'post\',
\'has_archive\' => true,
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => [ \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' ],
\'taxonomies\' => [ \'genre\' ],
];
register_post_type( \'book\', $book_args );
$movie_args = [
\'label\' => __( \'Movies\', \'textdomain\' ),
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => [ \'slug\' => \'movies\' ],
\'capability_type\' => \'post\',
\'has_archive\' => true,
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => [ \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' ],
\'taxonomies\' => [ \'genre\' ],
];
register_post_type( \'movie\', $movie_args );
$game_args = [
\'label\' => __( \'Games\', \'textdomain\' ),
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => [ \'slug\' => \'games\' ],
\'capability_type\' => \'post\',
\'has_archive\' => true,
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => [ \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' ],
\'taxonomies\' => [ \'genre\' ],
];
register_post_type( \'game\', $game_args );
}
基本URL示例基于以上设置,我们将立即获取这些URL:
Post type archive URLs
Single post type URLs
Taxonomy Term archive URLs
- http://example.com/genres/{流派术语slug}
(此存档将包括all 文章类型,这不是我们所追求的)必须添加重写规则处理重写规则,以便将特定体裁术语限制为单个文章类型。每种帖子类型的分页都需要一个额外的规则。/**
* Add rewrite rules for genre terms limited to book, movie, and game post types.
* Pagination issue fix via http://wordpress.stackexchange.com/a/23155/2807
* @link https://codex.wordpress.org/Rewrite_API/add_rewrite_rule
*/
function wpse247013_rewrite_rules() {
// Book Genres
add_rewrite_rule( \'^books/book-genres/([^/]+)/?$\',
\'index.php?taxonomy=genre&post_type=book&term=$matches[1]\', \'top\' );
// Book Genres pagination
add_rewrite_rule( \'^books/book-genres/([^/]+)/page/([0-9]+)?$\',
\'index.php?post_type=book&genre=$matches[1]&paged=$matches[2]\', \'top\' );
// Movie Genres
add_rewrite_rule( \'^movies/movie-genres/([^/]+)/?$\',
\'index.php?taxonomy=genre&post_type=movie&term=$matches[1]\', \'top\' );
// Movie Genres pagination
add_rewrite_rule( \'^movies/movie-genres/([^/]+)/page/([0-9]+)?$\',
\'index.php?post_type=movie&genre=$matches[1]&paged=$matches[2]\', \'top\' );
// Game Genres
add_rewrite_rule( \'^games/game-genres/([^/]+)/?$\',
\'index.php?taxonomy=genre&post_type=game&term=$matches[1]\', \'top\' );
// Game Genres pagination
add_rewrite_rule( \'^games/game-genres/([^/]+)/page/([0-9]+)?$\',
\'index.php?post_type=game&genre=$matches[1]&paged=$matches[2]\', \'top\' );
}
add_action( \'init\', \'wpse247013_rewrite_rules\', 10, 0 );
确保通过访问刷新重写规则Settings > Permalinks 将此代码添加到插件或主题后。如果您使用的是插件,则可以使用register_activation_hook.自定义URL上面添加的重写规则将启用以下新URL: