0

我遇到了Wordpress网站的子页面上的单页面应用程序的重定向规则问题。我很直接地遵循了这套说明,并且仍然存在问题:https://wordpress.stackexchange.com/questions/193479/redirect-sub-pages-to-parent-without-changing-url将WordPress网址重定向到父页面而没有更改网址

子页面是业务地点的自定义发布类型。当有人访问http://business.com/hollywood-ca/contact时,它应该拉起http://business.com/hollywood-ca/,但url需要保持不变(URL的联系人部分是每个位置页面上的单页Vue.js应用程序的一部分,因此需要坚持)。这里是我的代码:

//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(); 
} 

问题是,当我访问http://business.com/hollywood-ca/contact页面重定向到http://business.com/hollywood-ca/防止我的单页应用从导航到联系人选项卡。

如果有帮助,我还写了一些功能,将我的网址从business.com/location/hollywood-ca更改为清洁程序business.com/hollywood-ca。我已经测试了这些没有这些功能的问题,并且仍然存在问题,所以我不认为它们是相关的。

回答

1

我在Wordpress Exchange上得到了答案。我需要使用自定义查询变量查询index.php而不是page_id

add_rewrite_rule(
    "^{$slug}/", 
    "index.php?location={$slug}", //changed query var to location 
    'top' 
); 
相关问题