我无法获取下一个网址的ID: http://www.example.com/json/compact/id/48/
我的重写代码是:
private function initHooks()
{
add_filter( 'generate_rewrite_rules', [$this,'jsonRules'] );
add_filter( 'query_vars', [$this,'jsonQueryVars'] );
add_action( 'template_redirect', [$this, 'jsonTemplate'] );
}
public function jsonRules( $wp_rewrite ) {
$wp_rewrite->rules = array_merge(
['json/(.+?)/id/(\d*)' => 'index.php?json=$matches[1]&id=$matches[2]'],
$wp_rewrite->rules
);
}
public function jsonQueryVars( $query_vars ) {
$query_vars[] = 'json';
return $query_vars;
}
public function jsonTemplate() {
echo "<pre style='position:relative;z-index:9999'>";var_dump( get_query_var( 'json' ), get_query_var( 'id' ));echo "</pre>";
}
get_query_var( 'json' )
正在返回所需的结果,但get_query_var( 'id')
为空
重写规则为['json/(.+?)/id/(\d*)' => 'index.php?json=$matches[1]&id=$matches[2]']
我错过了什么?
答案 0 :(得分:2)
尝试添加此
public function jsonQueryVars( $query_vars ) {
$query_vars[] = 'json';
$query_vars[] = 'id';
return $query_vars;
}