craft.app.request.getSegment(1) - GET FIRST SEGMENT OF THE CURRENT URL

PHOTO EMBED

Tue Nov 28 2023 13:30:07 GMT+0000 (Coordinated Universal Time)

Saved by @vankoosh

The first highlighted line is a call to one of Craft’s internal functions that looks at the current URI or path. If we were on the /blog/topics/road-trips or /blog pages, for instance, the first “segment” would just be blog. If we were on /about, the first segment would be about.

{# Get the first segment of the current URI: #}
{% set navSegment = craft.app.request.getSegment(1) %}

<nav>
  <ul>
    <li>
      <a href="{{ url('blog') }}" class="{{ navSegment == 'blog' ? 'active' : 'inactive' }}">Blog</a>
    </li>
    <li>
      <a href="{{ url('about') }}" class="{{ navSegment == 'about' ? 'active' : 'inactive' }}">About</a>
    </li>
  </ul>
</nav>
content_copyCOPY