最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

permalinks - add_rewrite_rule on default post type

programmeradmin1浏览0评论

I need to add some rewrite rules on my posts in order to have one or more urls for the same post. E.g /foo/post-name and bar/post-name should lead the same post. I'm using this function:

function addRewritePost() {
    add_rewrite_rule( '^foo/([^/]+)/?', 'index.php?name=$matches[1]', 'top' );
    add_rewrite_rule( '^bar/([^/]+)/?', 'index.php?name=$matches[1]', 'top' );
}
add_action('init','addRewritePost');

This code redirects from foo/post-name to /post-name and I don't want this kind of behaivor. How can I solve this issue?

I need to add some rewrite rules on my posts in order to have one or more urls for the same post. E.g /foo/post-name and bar/post-name should lead the same post. I'm using this function:

function addRewritePost() {
    add_rewrite_rule( '^foo/([^/]+)/?', 'index.php?name=$matches[1]', 'top' );
    add_rewrite_rule( '^bar/([^/]+)/?', 'index.php?name=$matches[1]', 'top' );
}
add_action('init','addRewritePost');

This code redirects from foo/post-name to /post-name and I don't want this kind of behaivor. How can I solve this issue?

Share Improve this question asked Jan 15, 2019 at 10:22 S MadryS Madry 293 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

This would be bad practice, since WordPress already has a functionality to do this built in. Rewriting the built in functionality is never a good idea.

To achieve this the best way is to use Taxonomies.

Lets say you create two categories:

  1. Foo
  2. Bar

Now select the categories for the post in question and setup your permalinks structure to be /%category%/%postname%/ and you will get:

  • foo/post-name
  • bar/post-name

Hope this helps.

发布评论

评论列表(0)

  1. 暂无评论