Hot answers tagged

6

Untested, but something like this should work: {# set base criteria #} {% set params = { section: 'productsServices', limit: null } %} {# set relationship criteria #} {% set relationParam = ['and'] %} {% for segment in craft.request.segments %} {% if loop.index != 1 and segment != 'all' %} {% set category = craft.categories.slug(segment)...


6

Try using this (completely untested): {% if paginate.prevUrl %} <a href="{{ paginate.prevUrl }}?{{ craft.request.getQueryStringWithoutPath() }}">Previous Page</a> {% endif %} {% if paginate.nextUrl %} <a href="{{ paginate.nextUrl }}?{{ craft.request.getQueryStringWithoutPath() }}">Next Page</a> {% endif %} Update: The ...


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 ...


6

This is a longer version answer (code wise) to the one Douglas already posted. He did a very nice job of explaining how to get a URL parameter and what to pay attention to handling the results. I will try to show you how to build a query from multiple unknown params, so any of these combinations is possible and potentially return a single (and unique) Entry ...


5

You can access 'post' or 'get' parameters using craft.request.getParam('param'); and then use them to retrieve the corresponding entry. So, given the URL: http://example.com/subsection/filename.html?id="170" In your template, you would grab the param 'id', and use it to retrieve the entry. {% set entryId = craft.request.getParam('id') %} {% set entry = ...


4

Instead of using the chained syntax, you should build a parameter object and pass that in to craft.entries, like so: {# Get a start date value from the URL (my use case) #} {% set start_date = craft.request.getSegment(3) %} {# e.g. '2015-12-10' #} {# Create a parameter object, with default values #} {% set params = { section: 'events' } %} {# If ...


3

The behaviour you're seeing is by design – craft.request.getQuery() will return all GET variables if it's called without arguments, whereas craft.request.getParam() requires the name argument – i.e. craft.request.getParam('foo'). The changelog notes for build 1.3.2465 does reference a couple of issues related to these methods, but nothing suggesting any ...


3

If you want to explicitly grab them from PHP, this should also work: $result = Craft::$app->getRequest()->resolve(); From there, you could grab $result[1], which should be an array: array( 'variables' => array( 'post_id' => 3, , 'template' => 'post-entry', 'p' => 'post/3', )


3

It is passed as a parameter to your function public function actionView($post_id){ Craft::dd($post_id); } Note: your parameter must have the same name like defined in your route. You can't access it with public function actionView($postId)


3

The query value for p has been the pageTrigger since Craft 1.0 (and probably in the beta as well). I'm not sure why you were still able to use it for other purposes up until recently. To be honest, it sounds like you were accidentally exploiting a bug, which has since been fixed.


3

Your syntax is a bit off – the search() parameter doesn't accept multiple parameters, only a search query (e.g. .search('body:foobar')) or an object containing the search query plus search term options. In your case, you're actually passing an object (.search(entry.clientsOrAgency)), in which case Craft will look for a query parameter on that object (the ...


3

You’re just missing the : at the end of :notempty: in your second example. This will work: {% set params = { section: 'sectionName', customField: ':notempty:', limit: 10} %}


3

The client must have added a new rule which was causing the problem. changed from RewriteRule (.+) index.php?p=$1 [L] to RewriteRule (.+) index.php [L] Not exactly sure why they added that but...


2

The only possible core solution to query for these entries in Twig is to query for entry IDs first, process them with merge or without and then pass the IDs to the id parameter of a new ElementCriteriaModel. Other than your solution implementing a conditional in the for loop this works with paginate and getNext tags, you only query for the entry models you'...


2

You are currently passing a string '>= pers' to the array. Change this to: '>= ' ~ pers This "concatenates" the string '>= ' and the value of your pers variable (converted to a string).


2

I think we need more information to be able to consider possible solutions. I personally wouldn't do anything in Craft, rewriting this URL before it hits the application would be cleaner. I don't know if it's possible to configure the 'action' parameter name in Craft so let's assume not. Are the referring URLs always the same? In other words, is the visitor ...


2

You might be better off sending the values as an array, then you will only need one parameter. {# Change the macro to check for the value in an array #} {% macro is_active(segments) %} {% if craft.app.request.getSegment(2) in segments %}active{% endif %} {% endmacro %} {# Send an array to the macro #} <a href="/the-url" class="{{ ...


2

Your only problem here is limit but would something like this work for you? {# Default tags permitted #} {% set defaultTags = "<p>" %} {# Concatenate default + custom tags #} {% set allowedTags = defaultTags ~ matrixBlock.tagList ?? null %} {# Limit - use if provided or set to unlimited using a huge number #} {% set limit = matrixBlock....


1

I found the issue about this problem. I have in my code {% set selectedCategory = craft.app.request.getQueryParam('category') %} but I change getQueryParam with queryParams('category'); {% set selectedCategory = craft.app.request.queryParams('category') %} and the problem was solved


1

Thank you for the help August! I was able to get this work as intended. The only issue we ran into was getting an error for {type: entryTypes} when it was an an array of EntryTypes. Here is the finished code: {% set sections = [ 'pages', 'careerPaths', 'courses', 'events', 'eventsIndex', 'news', 'newsIndex', 'programs', 'allPrograms', 'facultyStaff', '...


1

I suspect the only way to do this at the moment would be to look up the EntryTypes for each of the desired Sections, and filter just by those—ignoring the section query facet. For example: {# Map some Section handles into actual Section models, using the Sections Service: #} {% set sections = ['one', 'two', 'three'] | map((h) => craft.app.sections....


1

That's just the way forms work in html. If you don't want to use a POST method, (because that would make the results not URL-addressable at all), I'd recommend using an intermediary Twig template to perform a redirect based on what parameters exist - i.e., set your form action to search/process and in that template write a load of conditional statements to ...


1

OK, this should be a working code example: {% set queryParams = craft.app.request.queryParams %} {% for key, value in queryParams %} {% set url = siteUrl ~ craft.app.request.fullPath %} {% set arr = queryParams | filter((v, k) => k != key) %} {% if arr|length > 0 %} {% set url = url ~ '?' ~ arr|url_encode %} {% endif %} <a href="{{...


1

Here's something I've got for filtering news by author and by category. You might be able to re-work it for your needs? From a page URL being: http://project.com/news?&cat=CATEGORY&author=NAME {% set query = {} %} {% set categoryParam = craft.app.request.getParam('cat') %} {% set authorParam = craft.app.request.getParam('author') %} {% set ...


1

I think the answer you’re looking for is here. TL;DR: relatedTo method accepts a CategoryModel, not the title(slug) of a category.


1

This should do what you're looking for: {% set someEntries = craft.entries({ level: 1, id: '1,2,3,4,5', myOtherField: 'and, not blue, not green' }) %}


1

Based on your example and description, it's a bit hard to tell whether you are dealing with a category template or an entry template, because in your description you say that the category handle is called 'media', but in the example you are grabbing entries in the section called 'media'. Assuming you are on a category template (i.e. trying to create a ...


1

This should take care of it: {% set stage = craft.request.getParam('stage') %} {# check if stage is an array #} {% if stage is iterable %} {% for stageItem in stage %} {# make sure stageItem has content #} {% if stageItem is not empty %} {{ stageItem }} {# would output the selected stage.value #} {% ...


1

You need to get each parameter from the URL separately and repeat the steps you already did for "collection": Get search parameters from the URL query string Find categories that match the slugs in your query string And merge them into the relationParam array.


1

Hmm, I can't think of how you would do this in Twig. But you may want to jump into PHP and perform your query a little more manually... This answer provides a few good ways to do that. Specifically, I'd take a look into either a custom variable or a template hook. I'm personally a huge fan of the template hook... it feels like unlimited power in a tiny ...


Only top voted, non community-wiki answers of a minimum length are eligible