admin管理员组文章数量:1022743
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?
1 Answer
Reset to default 1This 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:
- Foo
- 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.
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?
1 Answer
Reset to default 1This 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:
- Foo
- 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.
本文标签: permalinksaddrewriterule on default post type
版权声明:本文标题:permalinks - add_rewrite_rule on default post type 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745509944a2153781.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论