Skip to main content
11 votes
Accepted

Select entries starting with same letter from alphabet list

First, you should create a dynamic route (something like "games/glossary/<slug>") and point it to a template. A dynamic route means you won't have to create a template for every letter. Note the ...
Mats Mikkel Rummelhoff's user avatar
9 votes
Accepted

How to get IP address without a plugin?

To make use of your visitors IP address, use {{ craft.request.getIpAddress() }} in Craft 2.5. In Craft 3.x, there is {{ craft.app.request.userIP }}.
Avant la lettre's user avatar
8 votes
Accepted

When to use "if" or "without"?

For your example, you should exclude the fictional towns in the initial query itself rather than filtering them out afterwards: {% set towns = craft.entries.section('towns').townFictional('not 1')....
Mike Pepper's user avatar
  • 4,391
6 votes
Accepted

If page conditions

Both titles can not appear in your condition(on the homepage). Always only one true statement is executed. To your problem: there is one global variable that solves your problem. {% if craft.request....
Dominik Krulak's user avatar
6 votes
Accepted

If page is enabled, show div

You can check the status of an entry with .status. https://craftcms.com/docs/templating/craft.entries#status This code should work! {% set jobsEntry = craft.entries.id(jobsId) %} {% if jobsEntry....
Jake Dohm's user avatar
  • 766
5 votes
Accepted

If YouTube in Redactor Then Wrap in Div

Check out the Retcon plugin! Retcon is a small Craft CMS plugin offering a series of easy-to-use Twig filters for manipulating HTML content. Specifically, it sounds like you need the wrap method: ...
Lindsey D's user avatar
  • 24.1k
5 votes
Accepted

How to check if matrix block type is empty or not

You can fetch the number of entries for the first type, check if you receive an empty array if so fetch matrix blocks of type B Craft 2 {% set blocksTypeOne = entry.matrixFieldHandle.type('...
Robin Schambach's user avatar
4 votes
Accepted

Is the length filter required when checking if a field has content or not?

You can probably get away with removing |length for most conditionals that check for the existence of content in a custom field and just check on entry.fieldHandle instead. One exception to this ...
Steve Rowling's user avatar
4 votes
Accepted

Parent based Selectable Entries Condition

Rendering Twig in the "Selectable Entries Condition" field setting isn't actually possible, but it will be in the upcoming Craft 4.4 release, which as of writing this (march 1st 2023) is due ...
Mats Mikkel Rummelhoff's user avatar
3 votes

Conditional check if Matrix block field is empty (no content)

I'm assuming your matrix field is not called hours? Because in your current code you are handeling it like it is. I think this should work {% for block in entry.matrixfieldHandle %} {% if ...
Jan_dh's user avatar
  • 858
3 votes
Accepted

Checking if a field contains information

If you want a Dropdown field with the option to not select a value (or to clear out the current selection), you can just add an option (typically the first) with an empty value column. If the empty ...
Mats Mikkel Rummelhoff's user avatar
3 votes
Accepted

Check if current entry is in loop and add class

Firstly, from the Craft docs on routing: If the URI matches an entry’s or category’s URI, the section’s/category group’s template will get loaded, and the matched element will be made available ...
Steve Rowling's user avatar
3 votes
Accepted

How can I filter selected/related entries by their categories?

Start by looping thru each category. Say, all categories in a category group called "eventsCategories." Then find entries related to each category (if any). {# loop through each of the categories #} {...
Alex Roper's user avatar
  • 2,642
3 votes

conditional does not run first in a file that is included using the extends feature for twig

The issue here is that extends isn't the same thing, neither conceptually or technically, as an include. When you extend a template with a layout template, the template is "decorated" by the layout. ...
André Elvan's user avatar
  • 7,288
3 votes

Avoid notice of variable's not being set within dev mode

Since Craft 2.6 you can do this cleanly with the ?? null-coalescing operator: {% if not (entry.title ?? false) %} ...
Marion Newlevant's user avatar
3 votes
Accepted

Does Craft parse requests inside a conditional if it is false?

Yes, Twig will parse it all down to PHP before running it. It stores the rendered PHP in a /storage/runtime/compiled_templates/ file. No, Craft will not run anything inside the conditional if it's ...
Lindsey D's user avatar
  • 24.1k
3 votes
Accepted

If multiple entry types?

You were so close! Instead of asking whether entry.type is equal to the array (which it isn't), you need to find out whether the value of entry.type matches one of the array items. Here is an updated ...
Jake Dohm's user avatar
  • 766
3 votes
Accepted

How to determine if there are events in a calendar?

Looking at the docs you can search for events for a certain calander by including the id or the handle in your query. {% set calendars = craft.calendar.calendars({ limit: 100, order: "name", ...
Robin Schambach's user avatar
3 votes
Accepted

Add conditional in a for loop

{% for type in entry.buildType %} {{ type }} {% if not loop.last %}/{% endif %} {% endfor %} See: https://twig.symfony.com/doc/2.x/tags/for.html#the-loop-variable
Luke Holder's user avatar
  • 6,897
3 votes

How do multiple date queries differ between Craft 2 and 3

My Craft 3 version is: {% set result = craft.entries() .section('events') .eventDate( ['and', '<' ~ now|atom, '>' ~ now|date_modify('-6 days')|atom ] ) .results(':notempty:') ....
Paul Frost's user avatar
  • 1,060
3 votes

Twig default filter or Null coalescing operator?

So things like the null coalescing operator make it easier to chain things, so if you have a number things to fall back on, it's much neater to write: {% set woof = bark ?? howl ?? growl ?? null %} ....
andrew.welch's user avatar
  • 11.6k
3 votes
Accepted

Check for number less than x in array

Twig isn't very well equipped to deal with more than basic logic and comparisons, but you can accomplish some of these tests with a bit of extra juggling: {% set constraintMet = false %} {% for ...
August Miller's user avatar
3 votes

Is there a way to make conditional input fields in Craft CMS?

For what it's worth, it's coming in Craft 4. The author of the Reasons plugin has specifically said he won't port it to Craft 3, as this will be part of core in the next release.
KSP's user avatar
  • 849
3 votes
Accepted

Tertiary Nav Items breaking Navigation when lightswitch set to zero

The issue is that the way you check whether the current entry has children is inconsistent. You're using the {% ifchildren %} tag, which will only check if the entry has children in the pages array, ...
MoritzLost's user avatar
  • 11.9k
2 votes

How can I add a dynamic "active" CSS class to the navigation on any given page?

I know this post is old, but I'd like to share another technique I've been using for the past year. If you need a "hard-coded" navigation with no editing capabilities in the admin but still want ...
mateostabio's user avatar
2 votes

Can't get checkboxfield values to show

If you want a comma-separated list of selected options, try this. {% for option in offer.offerMinimumPrice.options|filter(v => v.selected) %} {{ option.label }}{% if loop.last==false %}, {% ...
Critical Mash's user avatar
2 votes
Accepted

Conditionals and outputting tables

Assuming your example code, something along these lines will help... {% if entry.yourTable | length %} <table> {% for row in entry.yourTable %} <tr> <td>{{ row....
Steve Holland's user avatar
2 votes

How can I filter selected/related entries by their categories?

Craft’s |group filter can be used here. I’m going to assume that the Categories custom field on your Exhibitor entries is called myCategoriesField. {% set groupedListings = entry.myEntriesField.find(...
Brandon Kelly's user avatar
2 votes

How to add two variables into one conditional

It looks like according to your code this (and Aran's) solution should work except for one small difference... if you are testing for this: entry.title ~ category.title The value parameter (what is ...
Alan Miller's user avatar
2 votes

If page conditions

I like the solution above. I'd add craft.app.language if you're working on a localized site. {% if craft.app.request.absoluteUrl == siteUrl or craft.app.request.absoluteUrl == siteUrl + craft.app....
Leo Leoncio's user avatar

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