当您不知道查询字符串是否已启动时,可以使用add_query_arg它知道如何应对,并添加了“?
“或”&
“标记(需要哪一个)到查询字符串。
根据流行需求更新我添加了一些来自法典的示例:
Using get_permalink:
由于get\\u permalink()返回一个完整的URL,所以当您想向帖子页面添加变量时,可以使用它。
// This would output whatever the URL to post ID 9 is, with \'hello=there\' appended with either ? or &, depending on what\'s needed
echo add_query_arg( \'hello\', \'there\', get_permalink(9) );
more general:
假设我们在WordPress的URL上
"http://blog.example.com/client/?s=word"...
// This would output \'/client/?s=word&foo=bar\'
echo add_query_arg( \'foo\', \'bar\' );
// This would output \'/client/?s=word&foo=bar&baz=tiny\'
$arr_params = array ( \'foo\' => \'bar\', \'baz\' => \'tiny\' );
echo add_query_arg( $arr_params );
或者,如果要与您拥有的任何链接一起使用,您可以传递链接uri:
//say your link is: http://wordpress.stackexchange.com/questions/14827/
//then use:
echo add_query_arg( \'hello\', \'world\',\'http://wordpress.stackexchange.com/questions/14827/\');
获取
http://wordpress.stackexchange.com/questions/14827/?hello=world
Example plugin page URL with extra query args:
$query_args = array( \'page\' => \'your-plugin-page\', \'foo\' => \'bar\' );
echo add_query_arg( $query_args, admin_url( \'/options-general.php\' ) )
// outputs
// http://example.com/wp-admin/options-general.php?page=your-plugin-page&foo=bar