简体   繁体   English

Wordpress add_rewrite_rule不带参数的重定向

[英]Wordpress add_rewrite_rule Redirects without parameter

I'm want to redirect a custom URL same like example in docs here 我想重定向自定义网址,就像此处文档中的示例一样

EX: http://domain.com/find/324 to http://domain.com/?text=324 例如: http : //domain.com/find/324http://domain.com/?text=324

This is the code , for some reason it keeps redirect me to homepage without the parameter "text" in the URL.. 这是代码,由于某种原因,它一直将我重定向到主页,而URL中没有参数“ text”。

function custom_rewrite_basic() {
  add_rewrite_rule('find/(.+)/?', 'index.php?text=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_basic');

The "text" parameter is not a build in WoprPress function, i'm going to use it with custom code of mine. “文本”参数不是WoprPress函数的构建,我将其与我的自定义代码一起使用。

I flushed that links cache, still redirects to homepage without the parameters. 我刷新了该链接缓存,但仍然不带参数将其重定向到首页。

What i'm missing? 我想念的是什么?

You cannot add custom query vars directly. 您不能直接添加自定义查询变量。 Custom query vars have to be added using the query_vars filter to be parsed within rules. 必须使用query_vars过滤器添加自定义查询变量,以便在规则内进行解析。 You can add custom query vars as seen below. 您可以添加自定义查询变量,如下所示。

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' );

Then you get get the query vars like this: 然后,您将获得如下查询变量:

get_query_var('text')

check https://wordpress.stackexchange.com/questions/271931/add-rewrite-rule-parameter-is-not-received-by-the-page?rq=1 for more detail. 有关更多详细信息,请检查https://wordpress.stackexchange.com/questions/271931/add-rewrite-rule-parameter-is-not-received-by-the-page?rq=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM