WordPress Rewrite Rule to exclude specific slug

断了今生、忘了曾经 提交于 2020-02-23 06:46:39

问题


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');
}

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.

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.

Is this possible at 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.

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

Help and advice appreciated. Many thanks!


回答1:


Okay, I figured it out eventually.

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');
}

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

Hope this manages to help someone else.

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



来源:https://stackoverflow.com/questions/43044611/wordpress-rewrite-rule-to-exclude-specific-slug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!