get_query_var 返回错误值

问题描述 投票:0回答:1

我创建了一个具有不同参数的重写规则


add_rewrite_rule(
    '^shop/(\b(thanks)\b)/([0-9]+)/([0-9]+)$', 
    'index.php?pagename=shop&shop_endpoint=$matches[1]&ref=$matches[2]&key=$matches[3]', 
    'top'
);

add_filter('query_vars', function($vars){
    $vars[] = 'shop_endpoint';
    $vars[] = 'ref';
    $vars[] = 'key';
    return $vars;       
}, 10, 1);

我有以下网址 https://www.example.com/shop/thanks/2956789/6212/

如果我倾倒

get_query_var('schop_endpoint');
get_query_var('ref');
get_query_var('key');

输出是

  • 谢谢
  • 谢谢
  • 谢谢

而不是

  • 谢谢
  • 2956789
  • 6212

我没有看到错误,谁可以帮助我一点?

php wordpress
1个回答
0
投票

我认为问题是

\b
存在于您的正则表达式中,这导致整个输出为
thanks

更正代码:

add_rewrite_rule(
    '^shop/(thanks)/(\d+)/(\d+)$', 
    'index.php?pagename=shop&shop_endpoint=$matches[1]&ref=$matches[2]&key=$matches[3]', 
    'top'
);

add_filter('query_vars', function($vars){
    $vars[] = 'shop_endpoint';
    $vars[] = 'ref';
    $vars[] = 'key';
    return $vars;       
}, 10, 1);

此外,为了简单起见,我已将

[0-9]
更改为
\d
(正如@mickmackusa 在问题评论中所建议的那样)。

希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.