admin管理员组文章数量:1130349
I have a custom post type that I'd like to have a permalink slug based on its taxonomy. All the posts of this CPT have one, and only one, term always marked on this specific taxonomy.
That's my code:
function plugin_domain_register_post_type(){
add_rewrite_tag('%event_segment%', '([^&]+)');
register_post_type( 'event',
array(
'public' => true,
'rewrite' => array( 'slug' => '%event_segment%'),
'has_archive' => false,
'hierarchical' => false,
'supports' => array( 'title', 'thumbnail')
)
);
}
add_action( 'init', 'plugin_domain_register_post_type' );
function plugin_domain_permalinks($post_link, $post) {
if (is_object($post) && $post->post_type === 'event') {
$terms = wp_get_object_terms($post->ID, 'segments');
if ($terms) {
return str_replace('%event_segment%' , $terms[0]->slug, $post_link);
}
}
return $post_link;
}
add_filter('post_type_link', 'plugin_domain_permalinks', 10, 3);
Okay, so it actually works, the permalinks changed and it goes to the right post. However, all the posts and pages (everything that's not of this CPT) that do not have the %event_segment% tag on the slug will literally just display the home page.
I've noticed this happens because of the add_rewrite_tag('%event_segment%', '([^&]+)'); part. The filter doesn't seem to be the issue, since even without the rewrite tag, it works as intended, and modifies the permalinks.
I don't know if this is relevant, but I'm on a Varying Vagrant Vagrants development environment, and it uses nginx, not apache.
What's going on here? Is there something I did wrong or forgot to do?
I have a custom post type that I'd like to have a permalink slug based on its taxonomy. All the posts of this CPT have one, and only one, term always marked on this specific taxonomy.
That's my code:
function plugin_domain_register_post_type(){
add_rewrite_tag('%event_segment%', '([^&]+)');
register_post_type( 'event',
array(
'public' => true,
'rewrite' => array( 'slug' => '%event_segment%'),
'has_archive' => false,
'hierarchical' => false,
'supports' => array( 'title', 'thumbnail')
)
);
}
add_action( 'init', 'plugin_domain_register_post_type' );
function plugin_domain_permalinks($post_link, $post) {
if (is_object($post) && $post->post_type === 'event') {
$terms = wp_get_object_terms($post->ID, 'segments');
if ($terms) {
return str_replace('%event_segment%' , $terms[0]->slug, $post_link);
}
}
return $post_link;
}
add_filter('post_type_link', 'plugin_domain_permalinks', 10, 3);
Okay, so it actually works, the permalinks changed and it goes to the right post. However, all the posts and pages (everything that's not of this CPT) that do not have the %event_segment% tag on the slug will literally just display the home page.
I've noticed this happens because of the add_rewrite_tag('%event_segment%', '([^&]+)'); part. The filter doesn't seem to be the issue, since even without the rewrite tag, it works as intended, and modifies the permalinks.
I don't know if this is relevant, but I'm on a Varying Vagrant Vagrants development environment, and it uses nginx, not apache.
What's going on here? Is there something I did wrong or forgot to do?
本文标签: custom post typesaddrewritetag broke permalinks that doesn39t use that specific tag
版权声明:本文标题:custom post types - add_rewrite_tag broke permalinks that doesn't use that specific tag 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749139700a2321918.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论