您可以先实例化数组,也可以选择填充它,然后将其传递给add_query_arg()
功能如下:
$url_query_args = array();
if( isset( $search ) ) {
$url_query_args[\'search\'] = $search;
}
if( isset( $category ) ) {
$url_query_args[\'category\'] = $category;
}
if( isset( $filter ) ) {
$url_query_args[\'filter\'] = $filter;
}
esc_url( add_query_arg( $url_query_args, \'/site\' ) );
或者,您可以循环使用可能的查询参数数组:
$url_query_args = array();
$possible_args = array(
\'search\',
\'category\',
\'filter\',
);
foreach( $possible_args as $arg ) {
if( ! isset( ${$arg} ) ) {
continue;
}
$url_query_args[ $arg ] = ${$arg};
}
esc_url( add_query_arg( $url_query_args, \'/site\' ) );