php WordPress get_query_var()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20379543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:07:56  来源:igfitidea点击:

WordPress get_query_var()

phpwordpress

提问by Themba Clarence Malungani

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.

我正忙于开发 WordPress 应用程序,我需要能够使用 WordPress 函数传递 url 参数。我使用add_query_arg()函数添加一个 url 参数。但是,当我尝试使用get_query_var()任何内容获取其他页面中传递的值时,将返回。当我使用$_GET['var_name']这些值时会返回。

What is the possible cause of this situation? I can successfully add arguments to the url but I am not able to access them.

造成这种情况的可能原因是什么?我可以成功地向 url 添加参数,但我无法访问它们。

回答by Themba Clarence Malungani

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.

我设法使该get_query_var()功能正常工作。要成功使用这两个函数,您需要将查询变量添加到 wordpress 的查询变量数组中。这是一个代码示例。

function add_query_vars_filter( $vars ){
  $vars[] = "query_var_name";
 return $vars;
}

//Add custom query vars
add_filter( 'query_vars', 'add_query_vars_filter' );

Now you can use get_query_var()and add_query_arg()as follows:

现在您可以使用get_query_var()add_query_arg()如下:

Add the query var and value

添加查询变量和值

add_query_arg( array('query_var_name' => 'value'), old_url );

Get the query var value

获取查询 var 值

$value = get_query_var('query_var_name');

More information and code samples can be found at the Codex: get_query_varand add_query_arg

更多信息和代码示例可以在 Codex 中找到:get_query_varadd_query_arg

回答by kinghfb

If you check out the Codex, you'll see you actually need to do some fiddling to get WP to start reading your query string.

如果您查看 Codex,您会发现您实际上需要进行一些操作才能让 WP 开始读取您的查询字符串。

Codex(under Custom Query Vars)

Codex(在自定义查询变量下)

Excerpt:

摘抄:

In order to be able to add and work with your own custom query vars that you append to URLs (eg: "mysite com/some_page/?my_var=foo" - for example using add_query_arg()) you need to add them to the public query variables available to WP_Query. These are built up when WP_Query instantiates, but fortunately are passed through a filter 'query_vars' before they are actually used to populate the $query_vars property of WP_Query.

为了能够添加和使用您附加到 URL 的自定义查询变量(例如:“mysite com/some_page/?my_var=foo” - 例如使用 add_query_arg()),您需要将它们添加到公共WP_Query 可用的查询变量。这些是在 WP_Query 实例化时建立的,但幸运的是,在它们实际用于填充 WP_Query 的 $query_vars 属性之前,它们会通过过滤器“query_vars”。

回答by Atul Jindal

To troubleshoot, what variables are being used in the request use following code

要进行故障排除,请求中使用了哪些变量,请使用以下代码

global $wp_query;
var_dump($wp_query->query_vars);