简体   繁体   English

WordPress中的Wordpress重写规则和自定义可选参数

[英]Wordpress rewrite rules and custom optional arguments in url

I am trying to build a filtering functionality for a Wordpress website using add_rewrite_rules. 我正在尝试使用add_rewrite_rules为Wordpress网站构建过滤功能。 I have the following link example 我有以下链接示例

 example.com/offers-type-all-inclusive-from-germany-transportation-airplane.html

Using the following rewrite rule: 使用以下重写规则:

add_rewrite_rule('^offers-type-?([a-z0-9-]+)?-from-?([a-z-]+)?-transportation-?([a-z0-9-]+)?.html','index.php?filters=offers&type=$matches[1]&location=$matches[2]&transportation=$matches[3]','top');

I managed to redirect the user to a custom page template with the arguments stored as an array 我设法将用户重定向到自定义页面模板,并将参数存储为数组

array(
    'type' => 'all-inclusive',
    'from' => 'germany',
    'transportation' => 'airplane'
     );

The problem is that some of the arguments could be optional so that links like 问题在于某些参数可能是可选的,因此像

example.com/offers-type-all-inclusive-from-germany.html
example.com/offers-type-all-inclusive-transportation-airplane.html

also need to build the correct filtering array. 还需要构建正确的过滤数组。

Is it even possible to achieve this? 甚至有可能实现这一目标?

What a tricky regex! 多么棘手的正则表达式!

Try this expression 试试这个表情

offers-type-([a-z0-9-]*?)(?:-from-([a-z-]*?))?(?:-transportation-([a-z0-9-]*?))?\.html

Here is an online demonstration 这是在线演示

http://regex101.com/r/fS4xB7 http://regex101.com/r/fS4xB7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM