Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [conditional]

Conditional will perform different actions depending on some condition.

1
vote
1answer
43 views

Twig default filter or Null coalescing operator?

When testing for a variable in a template these two appear to do the same thing. I've read CruftlessCraft's article on How to check for undefined variables but can't see an explanation of why I should ...
1
vote
1answer
33 views

How do multiple date queries differ between Craft 2 and 3

After spending a lot of time solving this for myself I thought I'd share it. In my Craft 2 template I had this query: {% set result = craft.entries({ section: 'events', eventDate: ['and', '&...
1
vote
1answer
43 views

Add conditional in a for loop

I need to add a separator if there are a multiple of types. How can this be done in a for loop? {% for type in entry.buildType %}{{ type }}{% endfor %} The outcome is similar to a breadcrumb nav, ...
2
votes
1answer
37 views

Check if page is Search, use different classes

I'm looking for a shorter way of writing this check. If the user visits the search page, apply the 3 column to the search-block, else use the 5 column: {% set currentPage = 'search' %} {% if ...
4
votes
1answer
357 views

How to check if matrix block type is empty or not

I have a matrix field with two different block types and I want create a conditional to check if one of the blocks is not empty show it and if it is show the other. This is not working: {% for block ...
1
vote
1answer
21 views

How to determine if there are events in a calendar?

I need to write a conditional that determines if events actually exist in a calendar. I only want to display an option in the the select dropdown IF the calendar has events: {% set calendars = craft....
1
vote
1answer
103 views

Dynamic Navigation with Condition to check slug

I'm trying to achieve one pretty simple thing, but putting it into my dynamic nav is making it a little difficult. I want to have a little number of the total amount of entries in my channel "listings"...
1
vote
0answers
35 views

chain of random entries based on relations

I'm developing a bio section for a client. Every bio is linked to another bio creating a chain of bios. The order of this chain should be random. The schema would be as following: Bios: Bio a, Bio b, ...
1
vote
1answer
72 views

Conditionally show table column headings

Is there a way to conditionally display a table column heading if the table field row has been entered? i.e. if my table has up to 3 columns and I fill in ALL the fields like this I'd see ch1 | ch2 |...
1
vote
1answer
103 views

Display variant title on cart page only for items with more than one variant

I want to display the variant title only on line items which have more than one variant. On the Image you can see the bold Product title and undeneath there is the variant title (Format A2). As you ...
2
votes
1answer
75 views

Conditional anchor in structure url

I have a structure section, where the last child should be an anchor in the slug: parent/parent#child I'm trying to setup conditional url formats on nested entries as such: {parent.uri}{% if object....
1
vote
1answer
90 views

Is defined conditional passing, yet causes a “do not have a method or closure named” error

Why is it that in the below, referencing stRow.caption causes an error despite being wrapped in an "if defined" check? The error is: Craft\SuperTable_BlockModel and its behaviors do not have a ...
2
votes
2answers
313 views

If page is enabled, show div

I'd like to show a 'Jobs' link in my navigation only if the Jobs page is enabled. Is there a way for craft to detect if the page is enabled, and show a div?
1
vote
2answers
293 views

If multiple entry types?

Is it possible to do the following? {% if entry.type == 'overview' %} {{ entry.title }} {% elseif entry.type == ['availability', 'specifications', 'sitemap', 'location'] %} {{ entry.title }...
2
votes
1answer
1k views

If page conditions

I have an {% include%} which displays entries from a structure. This {% include%} is displayed on the homepage and other entries throughout the site. On the homepage, I'd like the include to show a ...
0
votes
2answers
86 views

How to add two variables into one conditional

I'm using Freeform Pro to manually output a select field, but I think my question is a general Craft/Twig conditional question so I'm posting it here. The option values in my dropdown field are made ...
1
vote
1answer
41 views

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

Does Craft parse the contents of a false/negative/empty conditional statement? So would you take a performance hit if you had a big chunk of code inside a conditonal that was false? {% set foo = "foo"...
1
vote
1answer
412 views

For loop, if statement, then limit

I've created two global variables which my content creator is going to use to turn off a bunch of articles and turn on a bunch more which a slight change in the Global. I have a page where there is a ...
1
vote
1answer
82 views

Using AND with craft.entries()

I'm trying to return entries based on some criteria. I exclude entries with a given value like this: {% set someEntries = craft.entries({ level: 1, id: '1,2,3,4,5', myOtherField: 'not ...
2
votes
1answer
401 views

Make fields conditionally required

Is there a way to make fields conditionally required? I have a toggle field that is used to hide or show a section (see screenshot: "Section 3 enabled"). If the section is shown (toggle: on), the ...
1
vote
2answers
144 views

Conditional to see if an Entries field in a user profile contains a certain entry ID

I've got a user profile field named Books. This is an Entries field that contains entries from the Books channel. The idea is that users will save books to their their profile to show they have read ...
3
votes
1answer
938 views

Limit the for loop returns with an if statement

I've got a for loop working perfect with a limit of 5. Then I throw in the if statement to filter those entries that correspond with a global variable I have set. The problem is the if statement is ...
1
vote
0answers
35 views

Trying to display post based on two variables

I am setting three variables like so ... {% set featureEntryIds = craft.entries({ section: 'blog', featurePost: 1, limit: 1 }).ids() %} {% set articleClass = entry.id in featureEntryIds ? 'featured' ...
1
vote
0answers
82 views

Simpler way to code up a category conditional?

I've coded up conditional for categories. I'm still new to Twig and curious if there is a simpler way of coding the following. {% for feedItems in craft.entries.section('news').limit(null).offset(1)....
3
votes
2answers
793 views

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

I have a section called "Events" and an Entries field, we'll call myEntriesField to select related entries from another channel called "Exhibitors" On the Events page, I need to display the related ...
1
vote
2answers
665 views

Check if current entry is in loop and add class

I'm super new to both Craft and Twig so, apologies in advance!! I'm looking to determine if the current entry I'm on is in a loop being called on that page. I have loop that goes through the entries ...
1
vote
1answer
61 views

Comparison with an integer from a dropdown list not working

I'm not sure if this is a bug or not, but there is something weird going on if I try to compare an integer form a dropdown field to an integer in the template. In short, it doesn't work. I'm working ...
1
vote
1answer
562 views

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

I'm having a difficult time with what seems like a fairly straightforward problem: I'm listing businesses and their related hours of operation (days/times). I'd like to set up a conditional statement ...
4
votes
1answer
368 views

Select entries starting with same letter from alphabet list

I have a list of all my games using pagination. It works great: http://www.colorcomputergames.com/games However, I'd like to add links at the top of the page like: A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|...
1
vote
1answer
45 views

Conditionals and outputting tables

Need a hand on a conditional to see if my table has any data, and if it does, output the table. The second thing I need help is the code to output the table. When I look at tutorials, they always ...
1
vote
1answer
580 views

Checking if a field contains information

I have read docs on Craft CMS site and Googled some related articles but can't seem to get my head around checking for conditions for a field. For example, if I have a 'Fruit' field drop-down: Apple ...
1
vote
1answer
139 views

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

When checking if a field has content or not in my templates I usually write something along the lines of: {% if entry.fieldHandle|length %} <h1>{{ entry.fieldHandle }}</h1> {% endif %...
1
vote
1answer
66 views

getAllSections but not these specific sections

I'm looping over my sections to produce a section filter group. But I want to exclude at least 2 of the sections. I'd have thought I could do craft.sections.getAllSections('not 18') for example but ...
0
votes
1answer
85 views

Exclude entry based on segment

I am trying to exclude the current entry in a "Recommended Reading" section which works at level 1 with the ID tag, but I also need it to do the same at level 2 (children). `{% for entry in craft....
5
votes
1answer
302 views

When to use “if” or “without”?

When is it advisable or prudent to use without rather than a conditional statement to exclude something, eg a set of entries, based on one parameter? For example, say I have real towns and fictional ...
1
vote
1answer
119 views

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

I have this conditional included in a layout file I use within others, using the extends feature in twig. This is at the very top of _layouts/application.twig {% if not currentUser %} {% ...
2
votes
1answer
108 views

If YouTube in Redactor Then Wrap in Div

So I have been using this CSS Tricks responsive video trick for years. The problem I'm having is that this new Craft site I built imported data over from EE into a single Redactor field. Is there any ...
2
votes
2answers
244 views

Conditions in an if statement 'or'

I'm trying to do something like this for all the SEO meta info in my head {% if entry or category is defined %} ... {% endif %} What's the right way to code this? The reason for this is I have ...
2
votes
1answer
88 views

Email Messages – If current email type is html or text

I am adding a logo to the email message When someone creates an account: via Settings > Email > Messages, the image displays/renders correctly when viewed as "HTML / Rich Text" within mail ...
1
vote
2answers
949 views

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

This is so simple in ExpressionEngine and yet I've tried everything for several hours and can't find a way to get it working in Craft??? I have 2 User Groups: userGroup1, userGroup2 If the currently ...
1
vote
2answers
94 views

Login test for user's group?

I want to display some content only if a user from a certain group (wholesalers) is currently logged-in. The following syntax seems to work - but is there something more shorthand than the nested ...
1
vote
1answer
674 views

Set a Variable inside a conditional

I'm building my first Craft site and need to set a variable if a field has an image and then use that variable elsewhere in the template. The field is part of a matrix and will repeat. What I have ...
1
vote
1answer
75 views

DRYer way to set column widths than with if statement?

Craft n00b here. Really loving it so far, but am wondering if how I'm implementing this is the best option, or if maybe there's a more sophisticated solution. Inside our content builder switch ...
1
vote
1answer
328 views

How do I show something only for a parent segment and not it's children?

How do I display something only for one of the parent Structure sections and not others or this sections children? I was thinking of using {% if craft.request.segment(2) == 'modules' and craft....
-1
votes
1answer
63 views

Conditional being ignored?

I have subscriptions setup with Charge and Member Groups setup for reach subscription. The handle and name of both the subscriptions as well as the related member groups is identical. Some of them ...
4
votes
1answer
277 views

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

I've got a blog section on a page but want to hide it if there are no blogs. {% set blog = craft.entries.section('blog').limit(5) %} {% if blog !='' %} <div class="something"> {% for ...
1
vote
1answer
244 views

Matrix Field Conditional

I'm trying to attach a custom header image for each entry, if none is selected then the default one will be displayed. contentBuilder is the Matrix field. imgHeader is an assets block type. img is ...
1
vote
1answer
244 views

Streamlining a loop with a bunch of conditions

I'm sure there must be a tidier and more efficient way of doing this so thought I'd throw it out there for some opinions. Building a site that has the usual lists (in grid format) for Blog, Gallery, ...
1
vote
1answer
53 views

Having a plugin trigger on entries.onSaveEntry if a Lightswitch is turned on

Essentially I want the plugin to trigger on entries.onSaveEntry which I have working fine at the moment. I want to add a Lightswitch field that will be a condition however. i.e. The plugin will only ...
3
votes
2answers
146 views

Are there any tradeoffs if you use the null-coalescing operator in place of first()?

With the addition of the null-coalescing operator (??) in Twig, it now appears that the following two examples both provide a way to retrieve the first entry and fallback to null if no entry exists: {...