WordPress 重写规则以排除特定的 slug [英] WordPress Rewrite Rule to exclude specific slug

查看:48
本文介绍了WordPress 重写规则以排除特定的 slug的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a rewrite rule in my functions.php which is working just fine.

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) 中.这个模板显示了一些自定义的东西,我们都很好.

This looks for anything like /offers/my-custom-path and then loads in a specific page template / ID (page_id=1948). This template displays some custom things and we're all good.

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

Whilst that's fine, I have a literal page on the URL path, /offers/all and I would like to exclude this from the rewrite rule.

这可能吗?

所以,换句话说,如果我们点击/offers/all,只需忽略规则并照常进行.如果有其他事情,请按照当前的规则执行.

So, in other words, if we hit /offers/all, just ignore the rule and carry on as normal. If anything else, do the rule as it currently works.

我想它需要对 Regex 进行一些调整,但我几乎不擅长模式匹配.

I suppose it needs some adjustment to the Regex, but I'm barely any good at pattern matching.

感谢帮助和建议.非常感谢!

Help and advice appreciated. Many thanks!

推荐答案

好吧,我最终想通了.

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

Working code that will ignore /offers/all but work with everything else:

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

关键部分,是将 (?!all) 添加到 Regex 中.

The key part, being the addition of (?!all) into the Regex.

希望这能帮助其他人.

重要:记住在 WordPress 中保存永久链接以刷新重写缓存!

Important: Remember to save the permalinks in WordPress in order to flush the rewrite cache!

这篇关于WordPress 重写规则以排除特定的 slug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆