【问题标题】:Redirect WordPress URLs to parent page without changing URL将 WordPress URL 重定向到父页面而不更改 URL
【发布时间】:2017-02-24 06:54:00
【问题描述】:

我在 Wordpress 网站子页面上的单页应用程序的重定向规则方面遇到问题。我非常直接地遵循了这组说明,但仍然遇到问题:https://wordpress.stackexchange.com/questions/193479/redirect-sub-pages-to-parent-without-changing-url

子页面是针对营业地点的自定义帖子类型。当有人访问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/,这会阻止我的单页应用导航到联系人选项卡。

如果有帮助,我还编写了几个函数,将我的 URL 从 business.com/location/hollywood-ca 更改为更清洁的 business.com/hollywood-ca。我已经在没有这些功能的情况下测试了这些问题,但仍然有问题,所以我认为它们不相关。

【问题讨论】:

    标签: wordpress redirect url-rewriting single-page-application vue.js


    【解决方案1】:

    我在 Wordpress Exchange 上得到了答复。我需要使用我的自定义帖子类型的自定义查询变量而不是 page_id 来查询 index.php

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-17
      • 2023-03-13
      • 2016-06-05
      • 2013-05-31
      • 2017-11-27
      • 2011-01-02
      • 2013-02-08
      相关资源
      最近更新 更多