WordPress get_query_var() [英] WordPress get_query_var()
问题描述
我正忙于开发 WordPress 应用程序,我需要能够使用 WordPress 函数传递 url 参数.我使用 add_query_arg()
函数来添加一个 url 参数.但是,当我尝试使用 get_query_var()
在另一个页面中获取传递的值时,没有返回任何内容.当我使用 $_GET['var_name']
时,返回值.
I am busy developing a WordPress application and I need to be able to pass url parameters using WordPress functions. I use add_query_arg()
function to add a url parameter. However, when I try to get the passed value in the other page using get_query_var()
nothing gets returned. When I used $_GET['var_name']
the values gets returned.
造成这种情况的可能原因是什么?我可以成功地向 url 添加参数,但我无法访问它们.
What is the possible cause of this situation? I can successfully add arguments to the url but I am not able to access them.
推荐答案
我设法让 get_query_var()
函数工作.要成功使用这两个函数,您需要将查询变量添加到 wordpress 的查询变量数组中.这是一个代码示例.
I managed to get the get_query_var()
function to work.
To use the two functions successfully, you need to add the query vars to wordpress's query vars array. Here is a code sample.
function add_query_vars_filter( $vars ){
$vars[] = "query_var_name";
return $vars;
}
//Add custom query vars
add_filter( 'query_vars', 'add_query_vars_filter' );
现在您可以使用 get_query_var()
和 add_query_arg()
如下:
Now you can use get_query_var()
and add_query_arg()
as follows:
添加查询变量和值
add_query_arg( array('query_var_name' => 'value'), old_url );
获取查询变量值
$value = get_query_var('query_var_name');
更多信息和代码示例可以在 Codex 找到:get_query_var 和 add_query_arg
More information and code samples can be found at the Codex: get_query_var and add_query_arg
这篇关于WordPress get_query_var()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!