我想重定向一个自定义URL,就像这里的文档中的例子一样
例如:http://domain.com/find/324到http://domain.com/?text=324
这是代码,出于某种原因,它一直将我重定向到主页,URL中没有参数"text"。。
function custom_rewrite_basic() {
add_rewrite_rule('find/(.+)/?', 'index.php?text=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_basic');
"text"参数不是WoprPress函数中的内置函数,我将把它与我的自定义代码一起使用。
我刷新了链接缓存,仍然重定向到没有参数的主页。
我缺少什么?
不能直接添加自定义查询变量。必须使用query_vars过滤器添加自定义查询变量才能在规则中进行解析。您可以添加自定义查询变量,如下所示。
function wpd_add_query_vars( $qvars ) {
$qvars[] = 'text'; //I used the one in your example
return $qvars;
}
add_filter( 'query_vars', 'wpd_add_query_vars' );
然后你会得到这样的查询变量:
get_query_var('text')
检查https://wordpress.stackexchange.com/questions/271931/add-rewrite-rule-parameter-is-not-received-by-the-page?rq=1了解更多详细信息。