Questions tagged [conditional]

Conditional will perform different actions depending on some condition.

Filter by
Sorted by
Tagged with
1
vote
1answer
25 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, ...
0
votes
0answers
19 views

Upload Location Variables

I'm trying to use a single "image" field across sections as well as categories. The intention is to keep fields to a minimum and at the same time I'm also trying to keep the assets folder ...
1
vote
2answers
20 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
2answers
51 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
1answer
57 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
2answers
68 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
2answers
158 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
1answer
61 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
1answer
544 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
117 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
75 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
48 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
1k 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
48 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
163 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
39 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
136 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
294 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
123 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
136 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
2answers
612 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
684 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
2answers
2k 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
110 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
45 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
750 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
103 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
744 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
291 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
1answer
1k 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
40 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
93 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
2answers
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
2answers
1k 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
95 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
870 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
491 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
54 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
1k 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
235 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
89 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
101 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....
4
votes
1answer
325 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
165 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
2answers
225 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
381 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
107 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 ...
2
votes
2answers
1k 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
135 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
1k 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 ...