And*_*cer 4 wordpress url-rewriting
可见 URL:
/products/building-automation/details/productSKU
Run Code Online (Sandbox Code Playgroud)
应该指向真实的 WordPress 页面:
/products/details/?SKU=productSKU
Run Code Online (Sandbox Code Playgroud)
public function setup_frontend() {
add_rewrite_rule(
'products/(building-automation|lighting-controls)/details/([^/]*)',
'products/details/?SKU=$matches[2]',
'top'
);
// Other code logic...
}
Run Code Online (Sandbox Code Playgroud)
据我所知,$matches指向 中正则表达式部分的点(),因此在示例中,$matches[1]将是building-automation,并且$matches[2]将是productSKU。
我已经多次尝试过冲水规则。这是从类内部调用的,但这似乎不是问题。我将函数附加到init事件中。
我正在获取404模板。我究竟做错了什么?
事实证明,参数的第二部分必须是index.php。因为我试图指向products/...,所以它坏了。所以我更新了这个:
add_rewrite_rule(
'products/(building-automation|lighting-controls)/details/([^/]*)',
'?pagename=products/details&SKU=$matches[2]',
'top'
);
Run Code Online (Sandbox Code Playgroud)
它不起作用并指向正确的页面和模板。然而,这$matches部分仍然没有完成。事实上,$_GET['SKU']是通过 as '$matches[2]'。
小智 5
第一个问题是它指向不以 开头的东西index.php。不幸的是,它必须指向index.php?stuff。但您仍然可以使用页面名称,如下所示。
add_rewrite_rule(
'products/(building-automation|lighting-controls)/details/([^/]*)',
'index.php?pagename=products/details&SKU=$matches[2]',
'top'
);
Run Code Online (Sandbox Code Playgroud)
其次,您需要注册您的查询字符串,即SKU. 添加这个:
add_filter( 'query_vars', 'product_details_rewrite_filter' );
function product_details_rewrite_filter( $query_vars ){
$query_vars[] = 'SKU';
return $query_vars;
}
Run Code Online (Sandbox Code Playgroud)
你仍然会有问题。$_GET['SKU']会$matches[2],不会RIBU1C。要解决此问题,请更改$matches[2]为$2. 然后它将起作用:
add_rewrite_rule(
'products/(building-automation|lighting-controls)/details/([^/]*)',
'index.php?pagename=products/details&SKU=$2',
'top'
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4416 次 |
| 最近记录: |