Skip to main content

Questions tagged [conditional]

Conditional will perform different actions depending on some condition.

Filter by
Sorted by
Tagged with
1 vote
2 answers
31 views

Craft 4.8.0 : Hide some entries to a group of user

I have somes entries : POI-1 POI-2 POI-3 I have two groups of users : Group-1 Group-2 Needs : The Group-1 can edit only POI-1 The Group-2 can edit only POI-2 and POI-3 I see that I can set this ...
0 votes
0 answers
32 views

How to clear conditional field values when hidden?

In Craft CMS 5, I have several conditional fields that display or hide based on the value of a Category field (single selection). Depending on the selected category, different fields appear: Date, ...
0 votes
0 answers
28 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 ...
1 vote
1 answer
41 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 ...
1 vote
2 answers
304 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 |...
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 ...
14 votes
2 answers
3k views

A/B testing - how to in Craft?

What's your process for A/B testing a Craft entry? I'm curious about how you'd structure alternative content for a page within Craft and what tools you'd use to measure the results. Any comments ...
2 votes
2 answers
135 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 ...
1 vote
1 answer
56 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 ...
1 vote
1 answer
63 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 ...
1 vote
1 answer
65 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 ...
1 vote
1 answer
852 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 ...
2 votes
1 answer
306 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 ...
3 votes
2 answers
402 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 ...
1 vote
1 answer
118 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 ...
0 votes
0 answers
104 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/...
16 votes
4 answers
129k views

How does the negative (not) conditional work in Twig?

If I write: {% if entry.section not 'Homepage' %}Blabla {% endif %} this results in the error: Unexpected token "operator" of value "not" ("end of statement block" expected) But if I write: ...
1 vote
1 answer
158 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 ...
1 vote
1 answer
74 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, ...
1 vote
2 answers
292 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. ...
3 votes
3 answers
2k views

Check if a table field’s row has a value

We have a table field that has the default value of 1 row when a new entry is published. Unfortunately, we have {% if entry.tableField | length %} display markup and field content... {% endif %} ...
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 ...
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 ...
3 votes
2 answers
2k views

How to output a placeholder image if an Assets field is empty

I'm trying to output an Asset image if the Main Image field is populated, and if it's not populated, I'd like to output a placeholder image. But I'm having problems getting the conditional to work - ...
1 vote
1 answer
106 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 ...
2 votes
3 answers
1k views

How do I conditionally set active state in category navigation?

Here is the code I am trying to use to set an active state for category navigation: {% for category in categories %} <li {% if craft.request.segment(2) == "{{ category.slug }}" %}class=...
1 vote
2 answers
275 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.
1 vote
2 answers
371 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 ...
1 vote
3 answers
777 views

Can't get checkboxfield values to show

I'm trying to build a filter and for that I want my client to be able to select a few options, which I use as classes for in my filter. I chose for a checkbox field and set it up in the backend. Now ...
2 votes
1 answer
256 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 ...
6 votes
2 answers
3k views

How to get IP address without a plugin?

I just want to run a couple of conditionals depending on a visitor's IP address. How would I do that?
2 votes
1 answer
1k views

Test for empty RichText field

I have a rich text field which contains: <p><br></p> This is what is left when you delete all content of a redactor field. This can't be deleted without the HTML view of Redactor. ...
0 votes
1 answer
406 views

Conditional variables within a certain matrix block type

So I have a matrix field with 3 different block types. The first two block types have markup contained within their scope. The last type, has opening and closing tags for the first of type and last of ...
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 }...
35 votes
11 answers
51k views

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

Just wondering how people approach setting a class of current in their navigation. Is there anything in Craft or Twig where you can set a conditional to check if a page is the current page. Or do you ...
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 ...
1 vote
1 answer
182 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
1 answer
248 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
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 ...
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....
1 vote
1 answer
316 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
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, ...
3 votes
2 answers
856 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
1 answer
522 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
1 answer
225 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
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 ...
0 votes
2 answers
167 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 ...
2 votes
1 answer
947 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
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"...
3 votes
2 answers
2k views

If Conditional for Lightswitch in Matrix Field

I have a section that needs to display a certain block type (heroVideo) if a lightswitch (displayHeroVideo) within it is active. Otherwise, it should display a different block type (heroImage). How ...