【问题标题】:WordPress Rewrite Rule to exclude specific slugWordPress 重写规则以排除特定的 slug
【发布时间】:2017-03-27 19:17:09
【问题描述】:

我的functions.php 中有一个重写规则,它工作得很好。

function custom_offers_rewrite() {
  add_rewrite_rule('^offers/([a-z-_]+)/?', 'index.php?page_id=1948&offer_restaurant=$matches[1]', 'top');
}

这会查找类似/offers/my-custom-path 的任何内容,然后加载特定的页面模板/ID (page_id=1948)。这个模板显示了一些自定义的东西,我们都很好。

虽然这很好,但我在 URL 路径上有一个文字页面,/offers/all,我想将其从重写规则中排除。

这可能吗?

因此,换句话说,如果我们点击/offers/all,只需忽略规则并照常进行。如果有其他问题,请按照当前的规则执行。

我想它需要对正则表达式进行一些调整,但我几乎不擅长模式匹配。

感谢您的帮助和建议。非常感谢!

【问题讨论】:

    标签: php wordpress url-rewriting


    【解决方案1】:

    好吧,我终于想通了。

    将忽略 /offers/all 但与其他所有内容一起使用的工作代码:

    function custom_offers_rewrite() {
      add_rewrite_rule('^offers/(?!all)([a-z-_]+)/?', 'index.php?page_id=1948&offer_restaurant=$matches[1]', 'top');
    }
    

    关键部分,将(?!all) 添加到正则表达式中。

    希望这可以帮助其他人。

    重要提示:记得在 WordPress 中保存永久链接,以便刷新重写缓存!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 1970-01-01
      • 2015-09-29
      • 2020-07-29
      • 2017-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多