Skip to main content
formatting. Wrapped long lines for readability
Source Link
Marion Newlevant
  • 12.1k
  • 1
  • 23
  • 55

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.

<nav class="navbar navbar-default navbar-fixed-top {% 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 %}">

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.

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

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 %}">
added 366 characters in body
Source Link

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.

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

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.

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.

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

Conditional logic to add class for specific section entries

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.