1

I'm trying to write a conditional that adds a class of 'here' to a navigation menu item if segment_1 of the URL matches the URI of the entry I'm specifying. But it isn't outputting anything, so I'd appreciate any help with this, thanks. Here's my code:

{% set about_entry = craft.entries.id(17).first() %}

<ul>
  <li {% if craft.request.getSegment(1) == '{{ about_entry.uri }}' %}class="here"{% endif %}><a href="{{ about_entry.url }}">{{ about_entry.title }}</a></li>
</ul>

This conditional works fine if I change '{{ about_entry.uri }}' for a hard-coded 'about', but I want the URI to be dynamic in case it gets changed in the future/

2

In Twig you'll never, ever need to nest logic tags, within output tags or visa-versa.

The syntax you're looking for is:

{% set about_entry = craft.entries.id(17).first() %}

<ul>
    <li {% if craft.request.getSegment(1) == about_entry.uri %}class="here"{% endif %}><a href="{{ about_entry.url }}">{{ about_entry.title }}</a></li>
</ul>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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