admin管理员组文章数量:1130349
I have a couple custom post types registered = Staff, Properties
If I have a page with the slug archive-staff, how can I automatically redirect that page to the Staff CPT archive page with a php function?
This should also work for any page that starts with "archive-", automatically redirecting to the archive of any registered custom post type that matches the 2nd word of the slug.
So the custom archive-staff page would redirect to the Staff CPT archive page.
The custom archive-properties page would redirect to the Properties CPT archive page.
...and so on for for all custom post types only if their corresponding custom page exists.
I have a couple custom post types registered = Staff, Properties
If I have a page with the slug archive-staff, how can I automatically redirect that page to the Staff CPT archive page with a php function?
This should also work for any page that starts with "archive-", automatically redirecting to the archive of any registered custom post type that matches the 2nd word of the slug.
So the custom archive-staff page would redirect to the Staff CPT archive page.
The custom archive-properties page would redirect to the Properties CPT archive page.
...and so on for for all custom post types only if their corresponding custom page exists.
Share Improve this question asked Nov 2, 2018 at 18:18 Charlie WedelCharlie Wedel 233 bronze badges1 Answer
Reset to default 4There are many solutions for that..
1. .htaccess rules
You can put some redirect rules in your .htaccess file:
RewriteRule ^(.*)\-staff/$ /staff/? [L,R=301]
// some other rules
2. Using WordPress hooks
function my_redirect_function() {
global $wp;
if ( preg_match( '@staff/?$@', $wp->request ) ) {
wp_redirect( get_post_type_archive_link( 'staff' ) );
die;
}
// ... put other rules in here
}
add_action( 'template_redirect', 'my_redirect_function' );
3. Using Redirection plugin
You can use that plugin and define custom redirects based on regular expressions.
I have a couple custom post types registered = Staff, Properties
If I have a page with the slug archive-staff, how can I automatically redirect that page to the Staff CPT archive page with a php function?
This should also work for any page that starts with "archive-", automatically redirecting to the archive of any registered custom post type that matches the 2nd word of the slug.
So the custom archive-staff page would redirect to the Staff CPT archive page.
The custom archive-properties page would redirect to the Properties CPT archive page.
...and so on for for all custom post types only if their corresponding custom page exists.
I have a couple custom post types registered = Staff, Properties
If I have a page with the slug archive-staff, how can I automatically redirect that page to the Staff CPT archive page with a php function?
This should also work for any page that starts with "archive-", automatically redirecting to the archive of any registered custom post type that matches the 2nd word of the slug.
So the custom archive-staff page would redirect to the Staff CPT archive page.
The custom archive-properties page would redirect to the Properties CPT archive page.
...and so on for for all custom post types only if their corresponding custom page exists.
Share Improve this question asked Nov 2, 2018 at 18:18 Charlie WedelCharlie Wedel 233 bronze badges1 Answer
Reset to default 4There are many solutions for that..
1. .htaccess rules
You can put some redirect rules in your .htaccess file:
RewriteRule ^(.*)\-staff/$ /staff/? [L,R=301]
// some other rules
2. Using WordPress hooks
function my_redirect_function() {
global $wp;
if ( preg_match( '@staff/?$@', $wp->request ) ) {
wp_redirect( get_post_type_archive_link( 'staff' ) );
die;
}
// ... put other rules in here
}
add_action( 'template_redirect', 'my_redirect_function' );
3. Using Redirection plugin
You can use that plugin and define custom redirects based on regular expressions.
本文标签: custom post typesRedirect a page based on last word in slug
版权声明:本文标题:custom post types - Redirect a page based on last word in slug 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749206852a2332714.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论