admin管理员组文章数量:1328016
Suppose I have these WordPress pages:
page-A
page-B
page-C
and a published page page-main
.
My question:
The clients see and access only page-main
which is in the end virtual - it doesn't exist. Accessing this page-main
page should redirect to one of the above mentioned pages A-C, depending on some conditions which are not important for this question.
How can I achieve it?
Suppose I have these WordPress pages:
page-A
page-B
page-C
and a published page page-main
.
My question:
The clients see and access only page-main
which is in the end virtual - it doesn't exist. Accessing this page-main
page should redirect to one of the above mentioned pages A-C, depending on some conditions which are not important for this question.
How can I achieve it?
1 Answer
Reset to default 1So you need this to happen before Wordpress has output anything at all, otherwise you won't be able to send a redirect.
A way to achieve this is with the template_redirect
hook, documented here: https://codex.wordpress/Plugin_API/Action_Reference/template_redirect
The example from that docs page is more or less exactly what you want. I've edited it a little bit for your question:
function my_page_template_redirect() {
if ( is_page( 'page-main' ) ) {
if ( $some_condition ) {
wp_redirect( home_url( $pageA ) );
} else {
... other caeses here with redirects to other pages
}
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );
You'd need to add that to your functions.php or a plugin.
本文标签: Conditional redirect to several pages
版权声明:本文标题:Conditional redirect to several pages 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742237526a2438351.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论