你好 Wordpress 忍者,
我是 wordpress 开发的新手。我定义了一个重写规则(带有标签),类似于您在此处和此处的示例中可以找到的规则。
$wp_rewrite->add_rewrite_tag('%filterx_location%', '([^&/]+)', 'filterx_location=');
$wp_rewrite->add_rule('property-location/(.+)$','index.php?post_type=property&filterx_location=$matches[1]', 'top');
现在,问题是,当查询完成时(显示页面),未设置 filterx_location 参数。事实上,var_dump($_GET)
给了我一个array(0) { }
.
我有停电之类的东西,我在这里想念一些简单的东西,只是想不通:-/
任何帮助深表感谢!
更新
更奇怪的是,当我生成重写规则时:
$wp_rewrite->add_rewrite_tag('%filterx_location%', '([^/]+)', 'filterx_location=');
$permalink_prefix = 'property-location';
$permalink_structure = '%filterx_location%/';
$rewrite_rules = $wp_rewrite->generate_rewrite_rules($permalink_prefix.'/'.$permalink_structure, EP_ALL, true, true, true, true, true);
如果我打印它们,我会看到一堆生成的 url 匹配和重定向。其中之一是:
property-location/([^/]+)/page/?([0-9]{1,})/?$ => index.php?filterx_location=$matches[1]&paged=$matches[2]&post_type=property
我添加所有生成的网址:
foreach($rewrite_rules as $regex => $redirect) {
if(strpos($redirect, 'attachment=') === false) {
//add the post_type to the rewrite rule
$redirect .= '&post_type=property';
}
//turn all of the $1, $2,... variables in the matching regex into $matches[] form
if(0 < preg_match_all('@\$([0-9])@', $redirect, $matches)) {
for($i = 0; $i < count($matches[0]); $i++) {
$redirect = str_replace($matches[0][$i], '$matches['.$matches[1][$i].']', $redirect);
}
}
//add the rewrite rule to wp_rewrite
$wp_rewrite->add_rule($regex, $redirect, 'top');
}
如果我转到 URL,/property-location/madrid/page/2/
那么 query_vars 确实有 ["paged"]=> int(2)。但是 filterx_location 完全被忽略了!