2016-07-30 67 views
3

使用最新的Wordpress版本,我正在測試add_query_arg()函數。wordpress remove_query_arg不工作

我想改變這一點:

https://www.amazon.com/booktitle/dp/XX1234/ref=as_li_ss_tl?ie=UTF8& 
qid=1468851514&sr=8-1&keywords=there&tag=mytag-20#navbar 

這個

https://www.amazon.com/booktitle/dp/XX1234/ref=as_li_ss_tl?ie=UTF8&qid=1468851514&sr=8-1&keywords=there&tag=thenewtag-20#navbar 

(改變查詢 '標籤' 參數)

所以我用的是WP功能add_query_arg()正是如此:

$url = 'https://www.amazon.com/booktitle/dp/XX1234/ref=as_li_ss_tl?ie=UTF8&qid=1468851514&sr=8-1&keywords=there&tag=mytag-20#navbar'; 
$newtag = "thenewtag"; 
$url1= add_query_tag('tag', false, $url); // should remove the 'tag' key/value from the query 
$url2 = add_query_tag('tag', $newtag, $url1); // should add the new 'tag' value to the query 
echo $url1 ; // returns the original $url 
echo $url2 ; // returns 'https://www.amazon.com/booktitle/dp/XX1234/ref=as_li_ss_tl?ie=UTF8&qid=1468851514&sr=8-1&keywords=there&tag=mytag-20&tag=thenewtag#navbar' 

第一個標籤參數(value ='mytag')仍然存在,並且第二個標籤參數(value ='thenewtag')被添加(像它應該那樣)。

爲什麼不add_query_tag刪除第一個標籤參數,因爲文檔說它應該。

請注意,我嘗試過WP r emove_query_arg()函數,但它只是調用add_query_tag(),所以我只是使用add_query_arg()和'remove'語法。

另外esc_url(防止XSS)在這裏沒有使用(我在後面/其他地方照顧)。

回答

0

試試這個

$url1 = add_query_tag('amp;tag', false, $url); 
echo $url1;