11 votes

Show content if logged in user is in userGroup1 OR userGroup2?

According to the docs, the userModel.isInGroup() method takes a "user group model, its handle, or ID" – in other words, it can only test for membership in a single group, not several. What you want ...
Mats Mikkel Rummelhoff's user avatar
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

Show content if logged in user is in userGroup1 OR userGroup2?

Going to chime in here as I ran into a similar issue, and solved it a bit differently. Instead of using group handles I was using a dynamic set of Group IDs. However, the problem still remains that ...
Ian's user avatar
  • 564
6 votes
Accepted

How do I write a conditional for whether a section contains entries?

Your blog variable is an ElementCriteriaModel, which can be treated like an iterable array: {% if blog|length %} Content {% endif %}
Patrick Harrington's user avatar
6 votes
Accepted

Conditional to detect mobile devices with getUserAgent?

Unlike in PHP, it's not actually possible to break a for loop in Twig. To work around that fact, you can use the for loop to set a flag, and then put the conditional outside the loop: {% set ...
Mats Mikkel Rummelhoff's user avatar
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

Conditions in an if statement 'or'

You have to type out the complete statement for each condition, i.e. {% if entry is defined or category is defined %} ... {% endif %}
Mats Mikkel Rummelhoff's user avatar
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
  • 24k
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

If only one Matrix block show this, for more then one block show that

Thanks to Brad for asking me a question above, it made me realize I was checking for matrix fields and not the matrix block. My code is now working that I am checking for the correct element on the ...
johnHoysa's user avatar
4 votes

Problem with IF conditional not working

To test if a field exists, you'll have to use brackets notation, i.e. {% if entry['eventLevel'] is defined %} ... {% endif %} When the field exists, but there isn't a selected option, the Radio ...
Mats Mikkel Rummelhoff's user avatar
4 votes
Accepted

DRYer way to set column widths than with if statement?

One way to make it a bit more DRY and flexible is to use the split filter to create an array of column widths. Also, you can save a bit of markup by calculating the total number of columns, and using ...
Mats Mikkel Rummelhoff's user avatar
4 votes

Conditions in an if statement 'or'

Another way to approach this would be to standardize the different elements by passing them to a common included template with the same variable For entry pages: {% if entry is defined %} {% ...
Ben Parizek's user avatar
  • 13.4k
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
Accepted

Streamlining a loop with a bunch of conditions

I'm not sure if this is better or not but one thing I'd do here is to set some defaults in my include. This also means you don't need to worry about checking whether it's defined or not. You're just ...
Jamie Pittock's user avatar
3 votes
Accepted

Set a Variable inside a conditional

You're trying to your float variable using another variable at the moment that isn't defined. I expect you mean to set float to the string value "yes" like so: {% set float = "yes" %} You could also ...
Luke Pearce's user avatar
  • 3,863
3 votes
Accepted

Check if other locale exists

You can query for the same entry in another locale by explicitly setting the locale parameter. {% set de = craft.entries.id(entry.id).locale('de').first() %} {% set en = craft.entries.id(entry.id)....
carlcs's user avatar
  • 36.2k
3 votes

Problem with IF conditional not working

How about a conditional to check the entry.type rather than the field? Perhaps more appropriate in this case rather than checking if the field is defined. Something like this: {% if entry.type.handle ...
James's user avatar
  • 1,138
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

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

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

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,632
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
  • 24k
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

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