Skip to main content

Questions tagged [conditional]

Conditional will perform different actions depending on some condition.

Filter by
Sorted by
Tagged with
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 ...
Gareth's user avatar
  • 1,605
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: ...
KSPR's user avatar
  • 3,786
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 ...
Leah's user avatar
  • 441
9 votes
4 answers
8k 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?
noregt's user avatar
  • 1,062
8 votes
1 answer
876 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 ...
Niall Thompson's user avatar
7 votes
3 answers
317 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....
Clive Portman's user avatar
7 votes
1 answer
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 %}
Niall Thompson's user avatar
7 votes
1 answer
1k 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 ...
rppld's user avatar
  • 336
6 votes
2 answers
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"&...
KSPR's user avatar
  • 3,786
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?
Clive Portman's user avatar
6 votes
1 answer
290 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"...
Anna_MediaGirl's user avatar
5 votes
2 answers
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 %}...
MDLG's user avatar
  • 165
5 votes
1 answer
749 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. ...
Becky Soll's user avatar
5 votes
2 answers
925 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 ...
lealea's user avatar
  • 605
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
5 votes
1 answer
433 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 ...
Steve Adams's user avatar
  • 1,671
4 votes
2 answers
1k 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-...
Christopher Healey's user avatar
4 votes
1 answer
605 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
4 votes
1 answer
359 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 ...
Steven Thate's user avatar
4 votes
1 answer
575 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 }}" {% ...
KSPR's user avatar
  • 3,786
3 votes
3 answers
347 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 %} ...
Mark Busnelli Jr's user avatar
3 votes
3 answers
2k 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 ...
Paul Frost's user avatar
  • 1,060
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 %} ...
Steven Grant's user avatar
  • 1,855
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?
kingkelly's user avatar
  • 337
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 - ...
Stephen's user avatar
  • 685
3 votes
2 answers
271 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 ...
Daryl Campbell's user avatar
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 ...
Niall Thompson's user avatar
3 votes
2 answers
379 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. ...
Stuart McCoy's user avatar
3 votes
2 answers
854 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. ...
Alex H's user avatar
  • 955
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 ...
Mark Busnelli Jr's user avatar
3 votes
1 answer
432 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 ...
glider's user avatar
  • 647
3 votes
1 answer
324 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 ...
KSPR's user avatar
  • 3,786
3 votes
1 answer
39 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 (...
Martin's user avatar
  • 1,203
2 votes
2 answers
2k 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 ...
Shane Robinson's user avatar
2 votes
1 answer
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'...
Brian Feeney's user avatar
2 votes
2 answers
799 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 ...
sarah3585's user avatar
  • 899
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
2 votes
2 answers
278 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....
noregt's user avatar
  • 1,062
2 votes
2 answers
3k 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 ...
idahotallpaul's user avatar
2 votes
1 answer
3k 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....
KSPR's user avatar
  • 3,786
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. ...
KSPR's user avatar
  • 3,786
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
2 votes
1 answer
728 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 ...
KeironLowe's user avatar
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 ...
Mike Niland's user avatar
2 votes
1 answer
1k 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 ...
KSPR's user avatar
  • 3,786
2 votes
2 answers
704 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 ...
Mark Notton's user avatar
  • 2,337
2 votes
1 answer
696 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 ...
KSPR's user avatar
  • 3,786
2 votes
1 answer
391 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?
julzmon's user avatar
  • 941
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=...
jeff_nordstedt's user avatar
2 votes
1 answer
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 ...
osh's user avatar
  • 615