2

I'm trying to grab the query or param from an URL and then add it to an URL.

like products?price=15

How can I add the ?price=15 to a URL?

Like products/category?price=15?

6

Use craft.request.getQueryStringWithoutPath() like so:

products/category?{{craft.request.getQueryStringWithoutPath()}}

Returns the query string, except for the p= param (which was probably added by your .htaccess redirect).

More complicated logic to avoid the ? when the query string is empty (one of several ways to do this):

{# queryString will include '?' if we have one, otherwise '' #}
{% set queryString = (craft.request.getQueryStringWithoutPath() ? '?'~craft.request.getQueryStringWithoutPath()) %}
products/category{{queryString}}
|improve this answer|||||
  • 1
    But without query or param the URL's now have a "?" in it. Is it possible to conditionally hide or show the question mark depending if there is a query in the page url? – Johannes Lamers Mar 20 '15 at 18:12
  • 1
    Johannes, try something like this: {{ craft.request.getQueryStringWithoutPath() ? '?' ~ craft.request.getQueryStringWithoutPath() }}, or better: assign the query to a var first. – carlcs Mar 20 '15 at 19:11
  • @carlcs just beat me to it! – Marion Newlevant Mar 20 '15 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.