我在帖子类型注册流中到处搜索,但没有找到正确的方法(使用过滤器或操作)。
您可以使用不同的slug(我更喜欢)再次注册post类型,或者只运行WordPress运行的代码行来构建register\\u post\\u type中的重写规则。
我用slug注册了这个帖子类型book
并用这个技巧改变了它。我不确定它是否100%正确(因为我正在为相同的帖子类型重写规则),但它确实有效。
add_action(\'registered_post_type\', \'testbook\', 10, 2);
function testbook($post_type, $args) {
global $wp_rewrite;
if ($post_type == \'book\') {
$args->rewrite[\'slug\'] = \'changed_slug_book\'; //write your new slug here
if ( $args->has_archive ) {
$archive_slug = $args->has_archive === true ? $args->rewrite[\'slug\'] : $args->has_archive;
if ( $args->rewrite[\'with_front\'] )
$archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
else
$archive_slug = $wp_rewrite->root . $archive_slug;
add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", \'top\' );
if ( $args->rewrite[\'feeds\'] && $wp_rewrite->feeds ) {
$feeds = \'(\' . trim( implode( \'|\', $wp_rewrite->feeds ) ) . \')\';
add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . \'&feed=$matches[1]\', \'top\' );
add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . \'&feed=$matches[1]\', \'top\' );
}
if ( $args->rewrite[\'pages\'] )
add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . \'&paged=$matches[1]\', \'top\' );
}
$permastruct_args = $args->rewrite;
$permastruct_args[\'feed\'] = $permastruct_args[\'feeds\'];
add_permastruct( $post_type, "{$args->rewrite[\'slug\']}/%$post_type%", $permastruct_args );
}
}