get_query_var()获取当前查询的变量

目录

描述

Retrieve public query variable in the WP_Query class of the global $wp_query object.

get_query_var() 描述

用法

<?php get_query_var$var$default ?>

get_query_var() 用法

参数

$var

(string) (必填) The variable key to retrieve.

默认值: None

$default

(mixed) (可选) Value to return if the query variable is not set.

默认值: empty string

get_query_var() 参数

返回值

(mixed) 

returns $default if var is not set

get_query_var() 返回值

示例

Getting Current Pagination Number

For getting the current pagination number on a static front page (Page template) you have to use the 'page' query variable:

Note: The query variable 'page' holds the pagenumber for a single paginated Post or Page that includes the <!--nextpage--> Quicktag in the post content.

get_query_var() 示例

注意

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 (see below).

Custom Query Vars

In order to be able to add and work with your own custom query vars that you append to URLs (eg: "http://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 过滤器 'query_vars' before they are actually used to populate the $query_vars property of WP_Query.

So, to expose your new, custom query variable to WP_Query hook into the 'query_vars' 过滤器, add your query variable to the $vars array that is passed by the 过滤器, and remember to return the array as the output of your 过滤器 function. See below:

  • See WP_Query::get()
  • 使用到 global: (object) $wp_query

get_query_var() 注意

历史

添加于 版本: 1.5.0

get_query_var() 历史

源文件

get_query_var() 函数的代码位于 wp-includes/query.php.

get_query_var() 源文件

相关