WordPress get_query_var()

后端 未结 3 1661
闹比i
闹比i 2021-02-07 12:10

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 par

3条回答
  •  伪装坚强ぢ
    2021-02-07 12:29

    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' );
    

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

    Add the query var and value

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

    Get the query var value

    $value = get_query_var('query_var_name');
    

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

提交回复
热议问题