Hot answers tagged

11 votes
Accepted

Display entries if Field Date is past Now

You can combine a for and if statement in twig, so you don't necessarily need to use search. Something like this might get you a bit closer: {% for entry in craft.entries.section('event') if entry....
Steve Holland's user avatar
10 votes
Accepted

How to load entries via ajax when a filter is clicked?

There's a ton of ways to pull this off, but in my opinion setting up pages/URLs for each category is probably a good idea, for SEO and sharing reasons. It's not something that requires a lot of work ...
Mats Mikkel Rummelhoff's user avatar
10 votes
Accepted

Alphabetical index for a list of entries

First, sort all entries by multiple criteria: {# get our entries and order by firstname, lastname #} {% set allEntries = craft.entries.section('entries').orderBy('lastname asc, firstname asc').all() %...
Urs's user avatar
  • 639
10 votes
Accepted

How to order or filter entries by their author's custom field?

This requires a bit of extra work on the ElementQuery, but it can be efficiently done in the same query. We first perform an inner join on the author's content table using the authorId so that the ...
Ben Croker's user avatar
  • 7,266
7 votes
Accepted

Element API - 'String' to 'kebab-case'

You can actually use the StringHelper::toKebabCase function for this: $string = 'Hello there'; $result = StringHelper::toKebabCase($string); // => 'hello-there'
chris's user avatar
  • 906
7 votes
Accepted

Filtering multiple categories with URL queries

The issue with your current code is that you're looping over each type/size category, creating a parameter in your relatedTo query for each one. This means that you're currently creating a query that ...
Mats Mikkel Rummelhoff's user avatar
6 votes

How to apply multiple Twig filters to text - SOLVED

I believe filters are applied in the order they're written. So in your example you're probably applying the new line breaks with nl2br and then removing them again with hacksaw. Try: <p>{{ ...
Jamie Pittock's user avatar
6 votes

Display entries by category (multiple categories)

You can use .relatedTo(category) {% for category in craft.categories.group('categoryName') %} <h2>{{category.title}}</h2> {% for entry in craft.entries.section('news').relatedTo(...
Marco Schuler's user avatar
6 votes
Accepted

How to pull the first block of its kind from a Matrix?

You could try something like this: {% set block = post.buildStory.type('textEditorBlock').first() %} {{block.textEditor}}
alexr's user avatar
  • 598
5 votes
Accepted

How do I use URL parameters to filter a channel by a date field?

You can pull query string variables using the HttpRequestService, like this: {% set month = craft.request.query('month') %} {% set year = craft.request.query('year') %} Then, you'll need to ...
Mats Mikkel Rummelhoff's user avatar
5 votes
Accepted

How to display the title of a category based on its ID?

First you’ll need to grab the category ID from the query string, using craft.request.getQuery(): {% set catId = craft.request.getQuery('catId') %} Then you can fetch the CategoryModel via craft....
Brandon Kelly's user avatar
5 votes
Accepted

Filter entries by entry type in the CP?

What you can do is to create additional entry sources using the modifyEntrySources hook. This is a very simple example, that will add a heading and two sources to the end of the sources list (below ...
carlcs's user avatar
  • 36.2k
5 votes
Accepted

Count all unique values

Something like this should do the trick: {% set usersGroupedByCountry = craft.users.limit(null)|group('postaddress.country') %} <ul> {% for country, users in usersGroupedByCountry %} ...
Mats Mikkel Rummelhoff's user avatar
5 votes
Accepted

How to filter entries based on Tags

You can combine multiple relationship criteria by passing an array to the relatedTo parameter with the keyword 'and' or 'or' as the first array element. You want your entries to be related to any of ...
carlcs's user avatar
  • 36.2k
4 votes
Accepted

Trim trailing spaces before each new line in a multi-line string

Currently it’s not possible to pass regex modifiers to the |replace filter. You’d have to just do this manually: {% set str = "foo \nbar " %} {% set trimmed = "" %} {% for line in str|split("\...
Brandon Kelly's user avatar
4 votes
Accepted

Filter Sprout Forms entries based on field value, using parameters

Filtering entries is now possible as of Sprout Forms v2.2.0: {% for formEntry in craft.sproutForms.entries.formHandle('contact').firstName('Zoe') %} ... {% endfor %#}
Ben Parizek's user avatar
  • 13.4k
4 votes
Accepted

Reusing logic to set string variables

I would use macros. A Twig macro is basically a function, that can take arguments and render HTML. Macros can be defined inline (i.e. in the same template where it's used) or in a separate file (...
Mats Mikkel Rummelhoff's user avatar
4 votes
Accepted

Group by day does strange exeptions

When you specifically group on dateField.day (in your case, the |group('datum.day') part), the group filter won't care about the month or year for the date field – only the day. This means that any ...
Mats Mikkel Rummelhoff's user avatar
4 votes
Accepted

Using user-selectable timezones to show localized post dates

It is possible to work around the Craft 2 bug, by converting the Craft DateTime object to a native one. {% set craftDateTime = entry.postDate %} {% set nativeDateTime = date(craftDateTime|date('c')) %...
carlcs's user avatar
  • 36.2k
4 votes
Accepted

How can I let the user activate a particular SuperSort filter

So in order to do this you have to reformat your code and your strategy a bit. You cant provide a Craft query as a link, all that does is grab an array from the database and it won't inherently print ...
Alan Miller's user avatar
3 votes

Add '...' to template field

You might find the Hacksaw plugin useful for this https://github.com/ehousestudio/craft_hacksaw/ this will truncate text for you and has a wealth of options available for doing so, specifically the ...
Alec Ritson's user avatar
  • 4,519
3 votes
Accepted

Show a quote on my login page

Basically you want to retrieve all entries where a certain field is not empty? Return entries where field is "not empty"
Maarten de Graaf's user avatar
3 votes
Accepted

Getting Asset FolderId

You can always check the craft_assetfolders table in the database, find the folder name you're looking for and grab its id. And it's probably just a typo in your original code, you're setting the ...
Brad Bell's user avatar
  • 67.2k
3 votes
Accepted

Filtering entries by multiple categories with multiple filters

Passing the category as a comma delineated string and using craft.request.getParam('category') the second time will not grab the second variable 4rooms. First you need to parse that string into an ...
a-am's user avatar
  • 2,857
3 votes

How do you wrap text with HTML within a Plain Text field?

You wanna escape the product name before replacing. {{ product.name|escape|replace({'®': '<sup>®</sup>'})|raw }}
martin's user avatar
  • 29
3 votes
Accepted

Loading related entries by category using a select menu

You can do this using the relatedTo param. {# Get the selected category ID from a 'catId' query string param #} {% set catId = craft.request.getQuery('catId') %} {# Define the 'relatedTo' param ...
Brandon Kelly's user avatar
3 votes

Display entries if Field Date is past Now

Craft has changed since the above answer was given. The now object must be a part of the string, rather than concatenated as an object. so instead of: {% set futureEntries = craft.entries.section('...
ToddPadwick's user avatar
3 votes
Accepted

Order by fields after searching

Not really feasible without a plugin – fortunately, there's the excellent SuperSort: {% set results = craft.entries.section('player').search('team:"thenameoftheteam"') %} {% set ...
Mats Mikkel Rummelhoff's user avatar
3 votes
Accepted

Filter syntax for Dimensions & Weight?

There is currently no filter but you can access the units of measurement from your settings, and display the measurement: {% set weightUnits = craft.commerce.settings.weightUnits %} {% set ...
Luke Holder's user avatar
  • 6,767
3 votes
Accepted

Convert characters to Ascii

I would use a combination of things: Twig's replace filter to convert ä to ae etc. Twig's convert_encoding filter to eliminate any remaining non-ascii characters Craft's kebab filter to do its thing ...
Marion Newlevant's user avatar

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