How to change or Add url query in Hugo

For example have an url like https://www.anphabe.com or https://www.anphabe.com?utm_source=hugo. Now I want change url to https://www.anphabe.com?utm_source=hugo2&utm_campaign=hugo.
Here is my code:

{{ $u := urls.Parse "http://abc.com?test=1#tag" }}
{{ $u.Query Set "test2" "2" }}
{{ printf "%#v" $u }}

I got error: <$q.Set>: can’t call method/function “Set” with 0 results

Please help.
Thanks.

The result of the Hugo urls.Parse template function is immutable. You cannot use the Add, Del, or Set methods within a Hugo template.

You will have to construct the new URL manually using print or printf.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.