我创建了一个名为食品的自定义分类法。我使用用于WordPress的CPT UI插件做到了这一点。我想更改重写URL以根据分类标准而不是分类标准本身工作
例如:
我有一个名为Food的自定义分类法,附加到自定义职位类型的食物上。
现在为归档文件形成的URL是
example.com/food_item/bread
我希望它像
example.com/bread
我能够使用
做到这一点add_rewrite_rule(
'^bread/?$', // the rule regex
'index.php?taxonomy=food_item&term=bread', // where you want the rule to go
'top' // the priority. Make this one go first
);
但这不是动态解决方案。将来,除了面包以外,还有更多价值,我不想手动定义所有规则。是否有任何解决方案可以在任何分类中使用任何术语?两者都可能是动态的?
答案 0 :(得分:0)
function na_remove_slug( $post_link, $post, $leavename ) {
if ( 'food_item' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link; }
add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );
function na_parse_request( $query ) {
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'food_item', 'events', 'page' ) );
}
}add_action( 'pre_get_posts', 'na_parse_request' );
您可以尝试这种添加代码后需要更新永久链接的方式