[英]Wordpress rewrite rules and custom optional arguments in url
我正在嘗試使用add_rewrite_rules為Wordpress網站構建過濾功能。 我有以下鏈接示例
example.com/offers-type-all-inclusive-from-germany-transportation-airplane.html
使用以下重寫規則:
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');
我設法將用戶重定向到自定義頁面模板,並將參數存儲為數組
array(
'type' => 'all-inclusive',
'from' => 'germany',
'transportation' => 'airplane'
);
問題在於某些參數可能是可選的,因此像
example.com/offers-type-all-inclusive-from-germany.html
example.com/offers-type-all-inclusive-transportation-airplane.html
還需要構建正確的過濾數組。
甚至有可能實現這一目標?
多么棘手的正則表達式!
offers-type-([a-z0-9-]*?)(?:-from-([a-z-]*?))?(?:-transportation-([a-z0-9-]*?))?\.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.