admin管理员组文章数量:1415664
I have link:
<a href="'.esc_url(add_query_arg(array('search' => $search, 'category' => $category, 'filter' => $filter), '/site')).'">Click</a>
And some variables sometimes exist and sometimes don't, like $filter, which sometimes does not exist. Now I would like the variable does not exist, that it would not appear in the link, because the debugger displays the Notice "Undefined variable".
How should it be coded correctly?
I have link:
<a href="'.esc_url(add_query_arg(array('search' => $search, 'category' => $category, 'filter' => $filter), '/site')).'">Click</a>
And some variables sometimes exist and sometimes don't, like $filter, which sometimes does not exist. Now I would like the variable does not exist, that it would not appear in the link, because the debugger displays the Notice "Undefined variable".
How should it be coded correctly?
Share Improve this question asked Aug 26, 2019 at 14:04 manandmanmanandman 352 bronze badges1 Answer
Reset to default 1You can instantiate your array before-hand, optionally populate it, and pass it to the add_query_arg()
function like so:
$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' ) );
Or you could loop through an array of possible query args:
$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' ) );
本文标签: queryaddqueryarg() and empty variables inside
版权声明:本文标题:query - add_query_arg() and empty variables inside 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745211831a2647933.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论