You can rebuild the URL and append query variables to the URL query by using this function. There are two ways to use this function; either a single key and value, or an associative array.
Using a single key and value:
<code>add_query_arg( 'key', 'value', 'http://example.com' );</code>
使用关联数组:
add_query_arg( array( 'key1' => 'value1', 'key2' => 'value2', ), 'http://example.com' );
参数
$key : (string|array) (Required) Either a query variable key, or an associative array of query variables.
$value : (string) (Optional) Either a query variable value, or a URL to act upon.
$url : (string) (Optional) A URL to act upon.
用法举例:
(1)、
// 下面代码会输出 '/client/s=word&foo=bar' echo esc_url( add_query_arg( 'foo', 'bar' ) ); // 下面代码会输出 '/client/s=word&foo=bar&baz=tiny' $arr_params = array( 'foo' => 'bar', 'baz' => 'tiny' ); echo esc_url( add_query_arg( $arr_params ) );
(2)、
/* * This would output whatever the URL to post ID 9 is, with 'hello=there' * appended with either or &, depending on what's needed. */ echo esc_url( add_query_arg( 'hello', 'there', get_permalink( 9 ) ) );
(3)、
// 下面代码回输出 'http://blog.example.com/2009/04/16/hello=world' echo esc_url( add_query_arg( 'hello', 'world', 'http://blog.example.com/2009/04/16/' ) );
(4)、
$query = 'http://example.com/linkfoo=bar'; $new_query = add_query_arg( array( 'foo' => false, 'baz' => 'qux' ), $query ); print( $new_query ); // http://example.com/linkbaz=qux
(5)、
使用 add_query_arg 来获取当前页的完整地址 home_url( add_query_arg( null, null ));
你可能对这些文章感兴趣:
- WordPress函数:get_option
- WordPress调用指定友情链接分类目录下的链接名
- WordPress忘记后台密码怎么办?(附另类解决办法)
- 如何让单行的图片与文字或者input水平对齐
- WordPress升级到WordPress4.9的办法
- WordPress获取网站根目录、主题目录、插件目录路径和url地址
- xiu主题文章版权申明优化
- xiu主题自定义点赞文字
- WordPress快速建站第一讲:WordPress介绍及快速建站课程大纲
- WordPress开启上帝模式
- WordPress后台管理友情链接
- WordPress没有远程发布选项怎么办
- 零起点php入门第10课-php函数(1)
- 私人影院二次开发第一阶段完成
如有疑问,请前往问答中心反馈!
反馈