[英]Why isn't get_query_var returning my custom query vars?
I am building a WP plugin which defines some extra URLs, query vars and templates.我正在构建一个 WP 插件,它定义了一些额外的 URL、查询变量和模板。
I want to use get_query_var("wps_service_id");
我想使用
get_query_var("wps_service_id");
in my templates, but it is returning nothing no matter what I do.在我的模板中,但无论我做什么,它都不会返回任何内容。
I have whitelisted the extra query vars like the documentation explains :我已将额外的查询变量列入白名单, 如文档解释:
add_filter('query_vars', function ($query_vars) {
$query_vars[] = 'wps_service_id';
return $query_vars;
});
I am defining my extra URLs and loading the correct template like this:我正在定义我的额外 URL 并加载正确的模板,如下所示:
add_action('init', function () {
add_rewrite_rule(
'services/([a-z0-9-]+)/?$',
'index.php?wps_service_id=$matches[1]',
'top'
);
});
add_action('parse_request', function (&$wp) {
if (array_key_exists('wps_service_id', $wp->query_vars)) {
require plugin_dir_path(__DIR__) . "views/service.php";
exit();
}
return;
});
The template is loading, so WP is clearly reading and interpreting the query vars to some extent.模板正在加载,因此 WP 在某种程度上清楚地读取和解释了查询变量。 Why can't I read it and use it in the template using
get_query_var
?为什么我不能阅读它并使用
get_query_var
在模板中使用它?
Is it correct to use 'array_key_exists'?使用'array_key_exists'是否正确? You should use 'in_array', because you assign a value not a key in $query_vars[] = 'wps_service_id';
您应该使用“in_array”,因为您在 $query_vars[] = 'wps_service_id'; 中分配了一个值而不是键
Check with:检查:
error_log( array_key_exists('wps_service_id', $wp->query_vars));
error_log( in_array('wps_service_id', $wp->query_vars));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.