Wordpress 如何从 url 访问自定义查询变量
Wordpress how to access a custom query variable from the url
我有 url http://localhost/testsite1/coder2/?id=71
参数 id=71
.
因为我在 WordPress 中开发,所以我不能使用 $_GET['id']
,因此需要使用 get_query_var('id')
来访问参数。
然而这并没有产生任何结果。
当我通过 运行 代码查看所有变量时:
global $wp_query;
var_dump($wp_query->query_vars);
...我在任何地方都看不到 id
参数。
C:\wamp64\www\testsite1\wp-content\plugins\CSUKCODER\csukcoder.php:92:
array (size=64)
'page' => int 0
'pagename' => string 'coder2' (length=6)
'error' => string '' (length=0)
'm' => string '' (length=0)
'p' => int 0
'post_parent' => string '' (length=0)
'subpost' => string '' (length=0)
'subpost_id' => string '' (length=0)
'attachment' => string '' (length=0)
'attachment_id' => int 0
'name' => string 'coder2' (length=6)
'page_id' => int 0
'second' => string '' (length=0)
'minute' => string '' (length=0)
'hour' => string '' (length=0)
'day' => int 0
'monthnum' => int 0
'year' => int 0
'w' => int 0
'category_name' => string '' (length=0)
'tag' => string '' (length=0)
'cat' => string '' (length=0)
'tag_id' => string '' (length=0)
'author' => string '' (length=0)
'author_name' => string '' (length=0)
'feed' => string '' (length=0)
'tb' => string '' (length=0)
'paged' => int 0
'meta_key' => string '' (length=0)
'meta_value' => string '' (length=0)
'preview' => string '' (length=0)
's' => string '' (length=0)
'sentence' => string '' (length=0)
'title' => string '' (length=0)
'fields' => string '' (length=0)
'menu_order' => string '' (length=0)
'embed' => string '' (length=0)
'category__in' =>
array (size=0)
empty
'category__not_in' =>
array (size=0)
empty
'category__and' =>
array (size=0)
empty
'post__in' =>
array (size=0)
empty
'post__not_in' =>
array (size=0)
empty
'post_name__in' =>
array (size=0)
empty
'tag__in' =>
array (size=0)
empty
'tag__not_in' =>
array (size=0)
empty
'tag__and' =>
array (size=0)
empty
'tag_slug__in' =>
array (size=0)
empty
'tag_slug__and' =>
array (size=0)
empty
'post_parent__in' =>
array (size=0)
empty
'post_parent__not_in' =>
array (size=0)
empty
'author__in' =>
array (size=0)
empty
'author__not_in' =>
array (size=0)
empty
'ignore_sticky_posts' => boolean false
'suppress_filters' => boolean false
'cache_results' => boolean true
'update_post_term_cache' => boolean true
'lazy_load_term_meta' => boolean true
'update_post_meta_cache' => boolean true
'post_type' => string '' (length=0)
'posts_per_page' => int 10
'nopaging' => boolean false
'comments_per_page' => string '50' (length=2)
'no_found_rows' => boolean false
'order' => string 'DESC' (length=4)
我做错了什么?
评论不够space,我在这里解释一下!
From the documentation:
get_query_var()
only retrieves public query variables that are recognized by WP_Query
. This means that if you create your own custom URLs with their own query variables, get_query_var()
will not retrieve them without some further work.
您可以使用以下过滤器在 wordpress 中注册您的自定义 'query_var'
(代码片段进入 functions.php
文件):
add_filter('query_vars', 'registering_custom_query_var');
function registering_custom_query_var($query_vars)
{
$query_vars[] = 'id'; // Change it to your desired name
return $query_vars;
}
注册自定义后query_var
,就可以访问了!
global $wp_query;
var_dump($wp_query->query_vars);
我有 url http://localhost/testsite1/coder2/?id=71
参数 id=71
.
因为我在 WordPress 中开发,所以我不能使用 $_GET['id']
,因此需要使用 get_query_var('id')
来访问参数。
然而这并没有产生任何结果。
当我通过 运行 代码查看所有变量时:
global $wp_query;
var_dump($wp_query->query_vars);
...我在任何地方都看不到 id
参数。
C:\wamp64\www\testsite1\wp-content\plugins\CSUKCODER\csukcoder.php:92:
array (size=64)
'page' => int 0
'pagename' => string 'coder2' (length=6)
'error' => string '' (length=0)
'm' => string '' (length=0)
'p' => int 0
'post_parent' => string '' (length=0)
'subpost' => string '' (length=0)
'subpost_id' => string '' (length=0)
'attachment' => string '' (length=0)
'attachment_id' => int 0
'name' => string 'coder2' (length=6)
'page_id' => int 0
'second' => string '' (length=0)
'minute' => string '' (length=0)
'hour' => string '' (length=0)
'day' => int 0
'monthnum' => int 0
'year' => int 0
'w' => int 0
'category_name' => string '' (length=0)
'tag' => string '' (length=0)
'cat' => string '' (length=0)
'tag_id' => string '' (length=0)
'author' => string '' (length=0)
'author_name' => string '' (length=0)
'feed' => string '' (length=0)
'tb' => string '' (length=0)
'paged' => int 0
'meta_key' => string '' (length=0)
'meta_value' => string '' (length=0)
'preview' => string '' (length=0)
's' => string '' (length=0)
'sentence' => string '' (length=0)
'title' => string '' (length=0)
'fields' => string '' (length=0)
'menu_order' => string '' (length=0)
'embed' => string '' (length=0)
'category__in' =>
array (size=0)
empty
'category__not_in' =>
array (size=0)
empty
'category__and' =>
array (size=0)
empty
'post__in' =>
array (size=0)
empty
'post__not_in' =>
array (size=0)
empty
'post_name__in' =>
array (size=0)
empty
'tag__in' =>
array (size=0)
empty
'tag__not_in' =>
array (size=0)
empty
'tag__and' =>
array (size=0)
empty
'tag_slug__in' =>
array (size=0)
empty
'tag_slug__and' =>
array (size=0)
empty
'post_parent__in' =>
array (size=0)
empty
'post_parent__not_in' =>
array (size=0)
empty
'author__in' =>
array (size=0)
empty
'author__not_in' =>
array (size=0)
empty
'ignore_sticky_posts' => boolean false
'suppress_filters' => boolean false
'cache_results' => boolean true
'update_post_term_cache' => boolean true
'lazy_load_term_meta' => boolean true
'update_post_meta_cache' => boolean true
'post_type' => string '' (length=0)
'posts_per_page' => int 10
'nopaging' => boolean false
'comments_per_page' => string '50' (length=2)
'no_found_rows' => boolean false
'order' => string 'DESC' (length=4)
我做错了什么?
评论不够space,我在这里解释一下!
From the documentation:
get_query_var()
only retrieves public query variables that are recognized byWP_Query
. This means that if you create your own custom URLs with their own query variables,get_query_var()
will not retrieve them without some further work.
您可以使用以下过滤器在 wordpress 中注册您的自定义 'query_var'
(代码片段进入 functions.php
文件):
add_filter('query_vars', 'registering_custom_query_var');
function registering_custom_query_var($query_vars)
{
$query_vars[] = 'id'; // Change it to your desired name
return $query_vars;
}
注册自定义后query_var
,就可以访问了!
global $wp_query;
var_dump($wp_query->query_vars);