Questions tagged [conditional]

Conditional will perform different actions depending on some condition.

Filter by
Sorted by
Tagged with
35
votes
11answers
48k 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 ...
14
votes
4answers
96k 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: ...
14
votes
1answer
2k 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 ...
9
votes
4answers
7k views

Remove the last comma in an inline list of tags?

In a list of tags I'd like to remove the last comma. So is there something that works like the Ee backspace parameter?
8
votes
1answer
842 views

How Can I Include a Conditional for First Type of Matrix

I want to include a conditional for when the 'Text' block type is returned for the first time in a loop. In css it would be first-of-type. Here's what I have at the moment: {% if type == 'text' and ...
7
votes
3answers
310 views

Avoid notice of variable's not being set within dev mode

With dev mode on, I'm getting a warning there is no entry.title, which I know about already because that's what I'm testing for: {% if entry.title | length == 0 %} {% set entry = craft.entries....
7
votes
1answer
2k views

Conditionally Display Content if Section

How can I conditionally display content if its a specific section? Currently I'm trying this with no luck: {% if entry.section == 'lettings' %}{{ entry.rentFrequency.label }}{% endif %}
7
votes
1answer
927 views

Using an IF-statement to define the Entry URL Format

Within a channel I have two kinds of possible article types, which I separate with the entry type feature. For the entry URL format I want to include the type, but only for one of the two. I've tried ...
6
votes
2answers
7k views

How do I use divisibleby() in a for loop to display two columns?

I try to write a row in which there are two columns: <div class="row"> <div class="block_txt"></div> <div class="block_txt"></div> </div> <div class="row"&...
6
votes
2answers
2k 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?
6
votes
1answer
285 views

Is Shorter Twig Conditional possible?

Is there a way in Twig to make this conditional shorter so I don't use "craft.request.getSegment(2)" twice? {% if craft.request.getSegment(2) == "knowledge" or craft.request.getSegment(2) == "search"...
5
votes
2answers
1k views

Correct code for a conditional field?

What's the correct way to create a conditional so that a field won't display if it's empty? Is it: {% if entry.myentry | length %} ... {% endif %} or {% if entry.myentry %} ... {% endif %}...
5
votes
1answer
669 views

From a template, how can you order entries by one field, and if that field is empty a different field?

Is it possible to put conditions on the ordering of a loop of entries? I have two fields: title and alphaTitle. I want to order alphabetically, by alphaTitle if it has a value, otherwise by title. ...
5
votes
1answer
343 views

Is there a way to hide Matrix blocks based on the Selected Entry Type?

I have 2 primary entry types, one for a full width page, and one for a sidebar page. The sidebar page shares about 90% of the same block types as the full width page. The other 10% do not apply to ...
4
votes
2answers
959 views

Identify immediate siblings block type in matrix

I have a type of content that i use on the homepage of a site I am building, the code is shown here: <ul> {% for point in entry.keyPoints %} <li> <div class="a-...
4
votes
1answer
566 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|...
4
votes
1answer
347 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 ...
4
votes
2answers
867 views

How to use a global variable in a conditional statement if an entries relation field is empty?

{% if entry.fieldHandle %} {{ entry.fieldHandle}} {% else %} {{ global.fieldHandle}} {% endif %} Doesn't seem to be rendering for me. The entry field shows up when it's true, but the global field ...
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 ...
4
votes
1answer
555 views

Set parent nav to active

I have a navigation like this: {% set navi = craft.entries.id('2, 4, 5, 6' ).order('id asc') %} <nav> <ul> {% for entry in navi %} <li><a href="{{ entry.url }}" {% ...
3
votes
3answers
334 views

Conditional Parsing Issues

I'm not sure what's wrong with this original markup: Conditional One (doesn't work) {% if entry.metaTitle is defined %} {{ entry.metaTitle }} {% elseif title is defined %} {{ title }} {% else %} ...
3
votes
3answers
1k views

Problem with IF conditional not working

I have 2 sections, events and activities, that share many fields. But activities doesn't have an eventLevel field as there isn't a level. eventLevel is a radio fieldtype. So I want to insert the ...
3
votes
3answers
1k 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 %} ...
3
votes
2answers
744 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?
3
votes
2answers
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 - ...
3
votes
2answers
251 views

Homepage Takeover

I have a problem which may be very simple to solve, but I just can't get my head around it. I would like to do an alternative landing page if an image has been uploaded to the 'homepage' single ...
3
votes
2answers
1k 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 ...
3
votes
2answers
804 views

Unwanted output when using Twig conditional

I am displaying a series of icons and associated labels in an unordered list. Some of the icons should only be displayed when a user is in a specific set of user groups, so I am using a conditional. ...
3
votes
1answer
400 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 ...
3
votes
2answers
339 views

What's the best way to setup a conditional to see if a matrix block is used?

I need to add some javascript to a page depending upon whether or not a matrix block is used, one is for a slideshow block to load responsiveslides.js, another is for a video block to load fitvid.js. ...
3
votes
1answer
294 views

If loop contains id `X`

Is it possible to check a matrix array for an ID? I created a entries field which functions as a navigation. Now if the user decides to remove one entry I want the specific template to return a 404 ...
3
votes
1answer
32 views

Remove the anchor in nav loop based on another field conditional

I'm using this solution to output the children within a current structure entry. Unfortunately I need to take out the anchor on some entries that don't yet have content, but still include the (...
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 ...
2
votes
1answer
1k views

Comparison "or" conditional

I'm trying to use a conditional to do something if the first segment matches This or That. But I'm unable to come up with the right operator for the "or" {% elseif craft.request.firstSegment == 'This'...
2
votes
2answers
476 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
2answers
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 ...
2
votes
2answers
268 views

Setting a variable assets field, depending on the URL?

How can I set different assets fields depending on the URL? I tried: {% if craft.request.getSegment(1) == 'special' %} {% set assetfield = variantfield1 %} {% endif %} {% for asset in entry....
2
votes
2answers
2k views

Conditional Logic to show or hide fields in the admin?

I'm looking for a way to show or hide fields when editing an entry based on items selected. I need to manage a list of businesses and my plan is to have a channel to hold them all. But based on the ...
2
votes
1answer
2k views

Conditional to detect mobile devices with getUserAgent?

I want to create a User Agent check. (Yes I know. It is evil.) So I created an Array. The whole idea is to see if the user agent contains a specific word. This way I hope to circumvent regex trickery....
2
votes
1answer
938 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. ...
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 ...
2
votes
1answer
683 views

Config variable does not exist

I'm trying to set up caching and have it disabled for my local environment using {% cache unless craft.config.devMode %}. This works fine on non-local environments, but on my local environment I'm ...
2
votes
2answers
303 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
1answer
876 views

If date is between conditional

I want to check if it's winter. What is the best way to write a "between dates" conditional? {% set saison = now|date('m-d') %} {% if saison (saison >= '11-22' and saison <= '03-21') %} Jep ...
2
votes
2answers
691 views

How to link to a page, but only if the page status is active

This is probably a really basic question, but I can't figure out how to check the status of a page before I link to it. I have a link in my footer to 'Policy' page. But if the page status is ...
2
votes
1answer
611 views

Detect if a language is translated or not

For entries where the content isn't translated yet I'd like to add a "Sorry not translated yet" message. Since all the content is synced is there a way to check if the synced content is the same as ...
2
votes
1answer
349 views

How do I hide a login form if user is logged in?

What conditional statement would hide a form if a user is logged in?
2
votes
3answers
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=...
2
votes
1answer
2k views

Checking if a matrix field with entries has more than one entry

In my HTML-prototype I have two ways of showing a module of employees. One version shows one employee, and the other shows two. To build this in Craft I have setup a Matrix field (with a handle ...
2
votes
1answer
169 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 ...