Questions tagged [conditional]
Conditional will perform different actions depending on some condition.
136
questions
1
vote
1
answer
27
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 ...
2
votes
2
answers
88
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
52
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
60
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
51
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
277
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
169
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 ...
1
vote
1
answer
96
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
92
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/...
1
vote
1
answer
129
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
71
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
259
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.
...
1
vote
2
answers
84
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 ...
1
vote
1
answer
84
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 ...
1
vote
2
answers
252
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
344
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 ...
2
votes
1
answer
249
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 ...
1
vote
1
answer
2k
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
176
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
233
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
80
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
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 ...
1
vote
1
answer
64
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
304
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, ...
1
vote
2
answers
259
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
1
answer
499
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
214
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
157
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 ...
3
votes
2
answers
842
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
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 }...
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 ...
0
votes
2
answers
164
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
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"...
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 ...
1
vote
1
answer
109
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
1
answer
927
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
2
answers
553
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 ...
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 ...
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' ...
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)....
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 ...
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 ...
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 ...
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 ...
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|...
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 ...
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
...
1
vote
1
answer
763
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
1
answer
129
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 ...