admin管理员组文章数量:1122846
I am having an issue with redirect rules for a single-page app that is on a sub-page of a Wordpress site. I have followed this set of instructions pretty directly and am still having issues: Redirect sub-pages to parent without changing URL
The subpages are custom post types for business locations. When someone visits it should pull up
/
but the url needs to remain the same (The contact portion of the URL is part of a single-page Vue.js app on each location page, so it needs to stick around). Here is my code:
//This function hooks in on post save
function add_location_deep_links( $post_id, $post, $update ) {
$post_type = get_post_type($post_id);
if ( "location" != $post_type ) return; // If this isn't a 'location' post, don't update it.
$slug = $post->post_name; //hollywood-ca
add_rewrite_rule(
"^{$slug}/[A-Za-z]", //regex prevents trying to redirect the /hollywood-ca/ page
"index.php?page_id={$post_id}", //redirect to post
'top' // take precedence over other redirects
);
flush_rewrite_rules();
}
The problem is when I visit the page redirects to
/
which prevents my single-page app from navigating to the contact tab.
If it helps, I have also written a couple of functions that change my URLs from business/location/hollywood-ca
to the cleaner business/hollywood-ca
. I have tested these issues without those functions and am still having issues, so I don't think they are related.
I am having an issue with redirect rules for a single-page app that is on a sub-page of a Wordpress site. I have followed this set of instructions pretty directly and am still having issues: Redirect sub-pages to parent without changing URL
The subpages are custom post types for business locations. When someone visits http://business.com/hollywood-ca/contact
it should pull up http://business.com/hollywood-ca/
but the url needs to remain the same (The contact portion of the URL is part of a single-page Vue.js app on each location page, so it needs to stick around). Here is my code:
//This function hooks in on post save
function add_location_deep_links( $post_id, $post, $update ) {
$post_type = get_post_type($post_id);
if ( "location" != $post_type ) return; // If this isn't a 'location' post, don't update it.
$slug = $post->post_name; //hollywood-ca
add_rewrite_rule(
"^{$slug}/[A-Za-z]", //regex prevents trying to redirect the /hollywood-ca/ page
"index.php?page_id={$post_id}", //redirect to post
'top' // take precedence over other redirects
);
flush_rewrite_rules();
}
The problem is when I visit http://business.com/hollywood-ca/contact
the page redirects to http://business.com/hollywood-ca/
which prevents my single-page app from navigating to the contact tab.
If it helps, I have also written a couple of functions that change my URLs from business.com/location/hollywood-ca
to the cleaner business.com/hollywood-ca
. I have tested these issues without those functions and am still having issues, so I don't think they are related.
1 Answer
Reset to default 0Milo's comment got me there:
add_rewrite_rule(
"^{$slug}/",
"index.php?location={$slug}", //changed query var to location
'top'
);
本文标签: custom post typesRedirect subpage URLs to parent without changing URL
版权声明:本文标题:custom post types - Redirect sub-page URLs to parent without changing URL 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736289506a1928318.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
page_id
is only for the built inpage
post type. what is your CPT's query var? that's what you need to set. – Milo Commented Oct 14, 2016 at 18:37location
, what would be the change I need to make? – Jeff Commented Oct 14, 2016 at 18:40flush_rewrite_rules
is a labor-intensive function (regenerating the.htaccess
file) and should ideally only be called when something changes. – Jeff Commented Oct 14, 2016 at 19:08