如何解决Wordpress - 更改自定义帖子类型的永久链接
我有一个自定义帖子类型,可以为其分配位置。目前它的固定链接是:example.com/myposttype/post_name/
。我希望它是 example.com/post_location/myposttype/post_name/
。
我有以下代码:
function example_init() {
add_rewrite_tag('%post_location%','([^&]+)','post_location=');
add_rewrite_rule('^([^/]*)/([^/]*)/([^/]*)/?','index.php?post_location=$matches[1]&post_type=$matches[2]&name=$matches[3]');
}
add_action('init','example_init');
function example_post_type_link($permalink,$post,$leavename) {
if (stripos($permalink,'%post_location%') === false) return $permalink;
if (is_object($post) and $post->post_type == 'myposttype') {
$term = get_the_terms($post->ID,'location');
$slug = empty($term) ? "no-city" : urlencode($term->slug);
return str_replace('%post_location%',$slug,$permalink);
}
return $permalink;
}
add_filter('post_type_link','example_post_type_link',10,3);
因此,当我尝试转到 example.com/testcity/myposttype/testname/
时,WP 识别出这一点并且不显示页面 404 但是而不是仅显示页面,而是重定向到 example.com/myposttype/testname/
.
我希望这能被逆转。 example.com/testcity/myposttype/testname/
应该是主要(和规范)页面网址,example.com/myposttype/testname/
应该重定向到它。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。