Questions tagged [conditional]

Conditional will perform different actions depending on some condition.

Filter by
Sorted by
Tagged with
0 votes
0 answers
25 views

No option to add Credit Card Details field as a Conditional field

I'm trying to setup a payment integration having some slight issue with the conditional logic on Freeform. I'm on Craft CMS 3.8.12 (Pro Version and Freeform Pro). The form is supposed to have a ...
Armon's user avatar
  • 1
1 vote
1 answer
35 views

showing content conditionally in layout

I'm trying to conditionally show some content inside the layout file. In my entries I have a lightswitch field - to indicate which entries should be excluded. This all works fine when I can identify ...
glider's user avatar
  • 647
2 votes
2 answers
112 views

How to prevent Twig Syntax Error when a plugin is disabled?

I am using the Sprig plugin, and for some reason I'm disabling it in some environments. In config/general.php I have added: 'disabledPlugins' => ['sprig'] And in my twig template, I have a ...
Romain P.'s user avatar
  • 1,818
1 vote
1 answer
54 views

Looking to simplify this if else. Any tips to make this simpler?

This is my first post here. Is this a softball question? How could I simplify the following code in twig? I've placed the controls for having popUp Modal windows in Globals rather than put these ...
Dwayne Jamieson's user avatar
1 vote
1 answer
61 views

Tertiary Nav Items breaking Navigation when lightswitch set to zero

I have a navbar built out based on a pages section (structure) that has a field hideFromNav that when set to on hides the item from the nav bar - this is on line 8 of the code below. This works fine ...
CreateSean's user avatar
  • 1,973
1 vote
1 answer
58 views

Update from Craft 3 + Reasons to Craft 4: how to get fields' conditions from template?

I've just updated from Craft 3 to Craft 4. I was previously using the Reasons plugin for CP but also front-end purpose. On front-end, I was printing field layouts and show/hide the fields based on the ...
Romain P.'s user avatar
  • 1,818
1 vote
1 answer
539 views

Using "contains" to find a string within an entry

I am trying to use a conditional on an entry detail page to show a section only if a certain field contains a "t" in it. This is what I have but it doesn't appear to be working no matter ...
Megan's user avatar
  • 131
2 votes
1 answer
271 views

Parent based Selectable Entries Condition

In the Craft CMS admin, I have an entries field on a page, and I want to limit the selectable entries available to only the child pages of the current entry. To achieve this, I set the Selectable ...
Mike Niland's user avatar
1 vote
1 answer
114 views

Craft 4 - Upgrading Plugin to support Condition Fields

I've got a plugin that uses field layouts. I was asked about Craft 4 Conditional Field support so I went away to investigate. From what I gathered so far, the toggling of conditional fields happens ...
Sean Delaney's user avatar
0 votes
0 answers
100 views

Order Condition Formula in a Shipping Condition

I have digital products that are sold alongside physical products and need a way to set it so that the 'standard shipping' method is not shown as an option if ONLY digital products are in the cart/...
Christopher Healey's user avatar
1 vote
1 answer
136 views

How to get rules of conditional fields values on front-end, using Reasons or another solution?

I'm building a front-end form, where I'm displaying on front-end my fields following their layout within the CP: {% set section = craft.app.sections.getSectionByHandle('handle') %} {% set ...
Romain P.'s user avatar
  • 1,818
1 vote
1 answer
72 views

if entry has specific category, show specific html block

I have entries, which can have several categories. These categories are in different categorie groups. Now if i show an entry page, i want to display i certain html block based on a specific category, ...
Henrik's user avatar
  • 425
1 vote
2 answers
283 views

Conditional for specific page

I'd like to only load some scripts for 1 specific page with the Url /projects/. Currently I'm using: {% if craft.app.request.segments|first == 'projects' %} // load scripts {% endif %} This works. ...
David Flindall's user avatar
1 vote
2 answers
85 views

Correct Limit for Entries with IF statements

I have content on my site that can't be shown in some countries and requires a basic level of geo-targeting, based on the users profile and not by any form of IP geo-targeting. I have set up an ...
Chrisl2310's user avatar
1 vote
1 answer
99 views

how to check for the 2nd, 3rd, 4th… image in an asset's field

I am currently "translating" our current website, which runs on the lastest expressionengine, to craft. we use imagegrids that are arranged by css. so in an entrie's pulldown field choose between ...
Matthias's user avatar
1 vote
2 answers
267 views

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

I'd like to use conditional fields in my CMS instance. I found an old plugin called Reasons. I'm not sure it's compatible with Craft 3, so I'm looking for a workaround.
Valerio Volpe's user avatar
1 vote
2 answers
360 views

Using Conditionals in Title Format

I've got a new channel for Authors where there are 3 fields: firstName middleName lastName and I'm using these fields to generate the Title of the channel entry. However I would like to add a ...
CreateSean's user avatar
  • 1,973
2 votes
1 answer
251 views

Check for number less than x in array

I want to check if there is a number less than 10 in an array. {% if 10 in [0, 30, 50, 10, 9, 200] %} {# checks only if there is a 10 in it. not if there is anything below 10. #} yep. there is ...
KSPR's user avatar
  • 3,786
1 vote
1 answer
3k 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 ...
Paul Frost's user avatar
  • 1,060
1 vote
1 answer
180 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', '&...
Paul Frost's user avatar
  • 1,060
1 vote
1 answer
242 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, ...
Nutmeg's user avatar
  • 598
2 votes
1 answer
81 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 ...
TRIM's user avatar
  • 314
5 votes
1 answer
2k 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 ...
fodney's user avatar
  • 193
1 vote
1 answer
66 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....
Vaughn D. Taylor's user avatar
1 vote
1 answer
311 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"...
mateostabio's user avatar
1 vote
0 answers
44 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, ...
Max Frischknecht's user avatar
1 vote
2 answers
292 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 |...
Kev N's user avatar
  • 151
1 vote
1 answer
516 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 ...
KSPR's user avatar
  • 3,786
2 votes
1 answer
224 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....
Paul's user avatar
  • 339
1 vote
1 answer
158 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 ...
JamesNZ's user avatar
  • 937
3 votes
2 answers
852 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?
kingkelly's user avatar
  • 337
1 vote
2 answers
1k 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 }...
conormcafee's user avatar
2 votes
2 answers
3k 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 ...
Sam's user avatar
  • 421
0 votes
2 answers
165 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 ...
Stephen's user avatar
  • 685
1 vote
1 answer
64 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"...
Mark's user avatar
  • 731
1 vote
1 answer
1k 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 ...
afc's user avatar
  • 67
1 vote
1 answer
111 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 ...
Mario Vasquez's user avatar
2 votes
1 answer
937 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 ...
Sara's user avatar
  • 21
1 vote
2 answers
590 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 ...
Stephen's user avatar
  • 685
2 votes
1 answer
2k 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 ...
afc's user avatar
  • 67
1 vote
0 answers
44 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' ...
seth's user avatar
  • 117
1 vote
0 answers
98 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)....
Tad Ward's user avatar
  • 317
2 votes
2 answers
1k 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 ...
ByteMyPixel's user avatar
1 vote
2 answers
2k 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 ...
FraserKC's user avatar
1 vote
1 answer
106 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 ...
noregt's user avatar
  • 1,062
1 vote
1 answer
1k 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 ...
Adam's user avatar
  • 11
4 votes
1 answer
601 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|...
DevServe's user avatar
  • 303
1 vote
1 answer
65 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 ...
DevServe's user avatar
  • 303
1 vote
1 answer
2k 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 ...
DevServe's user avatar
  • 303
1 vote
1 answer
818 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 %...
Jamie Wade's user avatar
  • 3,644