1

I'm trying to create a conditional logic so that the class 'white-nav-bg' is added to the element on the 'blog' section entries only.

Below is the code I'm currently using to try and achieve this with a logic tag but it is adding the 'white-nav-bg' class to all pages on the site rather than just the blog section entry pages.

<nav class="navbar navbar-default navbar-fixed-top
  {% for entry in craft.entries.section('blog').limit(1) %}
    {% if entry.type.handle == 'post' %}white-nav-bg{% endif %}
  {% endfor %}">

This code is in the '_layout.html' file, hence needing to figure out how to do conditional logic tags rather than adding the code differently in the blog template files.

After much time looking through Craft documentation I can't seem to find an example of how to do this. Any help would be gratefully received.

UPDATE:

Following helpful answers I have come to the following code, which works and allows me to define multiple sections to have the 'white-nav-bg' class.

{% set segment1 = craft.request.segment(1) %}
<nav class="navbar navbar-default navbar-fixed-top
  {% if segment1 == 'blog' %}
    white-nav-bg
  {% elseif segment1 == 'our-work' %}
    white-nav-bg
  {% endif %}">

2 Answers 2

1

You could also do it based on segments:

{% set segment1 = craft.request.segment(1) %}

{% if segment1 == 'blah' %}
    do something
{% endif %}
5
  • Thanks, this worked. As Kaspar mentioned above, because it is in the _layout template this is a better approach. Is there a way to set multiple values in the 'blah' part? I'd like to do this for a few different sections. Sep 29, 2015 at 15:41
  • You can use getSegments then call each segment the same just changing the segment number in the example I gave you. Sep 29, 2015 at 16:36
  • Let me know if you need more info Sep 29, 2015 at 16:36
  • Thanks for the feedback Mark. So would I use the following to set each segment? {% set segment1 = craft.request.segment(1) %}{% if segment1 == 'blog' %}white-nav-bg{% elseif segment1 == 'ourWork' %}white-nav-bg{% endif %} Oct 1, 2015 at 11:22
  • Actually, I've just figured it out and the following code works: {% set segment1 = craft.request.segment(1) %}{% if segment1 == 'blog' %}white-nav-bg{% elseif segment1 == 'our-work' %}white-nav-bg{% endif %} I'll update the question with the code that worked based on your answer Oct 1, 2015 at 11:24
1

There are many possibilities for this.

One of them is to listen to the section handle if you want it to be triggered only on the blog template:

{% if entry.section.handle == 'blog' %}your-class{% endif %}
2
  • Hi Kaspar, huge apologies for delayed response. Thank you for your helpful answer. It has partially worked but I am getting a template error on the Blog page now "Variable "entry" does not exist" - I'm assuming this is because my code is in the _layout template before I have set entry? Sep 29, 2015 at 8:08
  • Yep. If you need this to be in the layout you can define a entry variable (Altough this isn't very pretty). {% if entry is undefined %}{% set entry == x%}{% endif %} But since you use it in layout I would go for the solution with the segments.
    – KSPR
    Sep 29, 2015 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.