我正在尝试为自定义帖子类型创建一个自定义永久链接,并将其仅应用于特定类别。例如,我想将此URL更改为:http://www.example.com/events/event-name 到http://www.example.com/webinars/event-name.
这是迄今为止我所掌握的代码,但它不起作用:
function change_webinar_links($permalink) {
global $post;
if(get_post_type() == \'tribe_events\' && has_category(\'webinar\', $post->ID)) {
$permalink = trailingslashit( home_url(\'/webinars/\' . $post->post_name ) );
}
return $permalink;
}
add_action( \'init\', \'webinar_rewrite_rule\');
function webinar_rewrite_rule() {
global $post;
if ( has_category(\'webinar\', $post->ID) ) {
add_rewrite_rule( \'^webinars/([^/]+)/?\', \'index.php?tribe_events=$matches[1]&post_type=tribe_events&name=$matches[1]\', \'top\' );
}
}