<p>Not tested, but I think you can test if it is a pagination page. So </p>

<pre><code>{% if (craft.request.getSegment('2') == '') or (craft.request.getPageNum() != 1) %}active{% endif %}
</code></pre>

CC BY-SA 3.0
2016-09-01T19:14:05.323Z
2016-09-01T19:14:05.323Z
AbbeyDesign
<p>Here is a possible macro. It uses <a href="https://craftcms.com/docs/templating/craft.request#getSegment" rel="nofollow">getSegment()</a> to get the second segment, and twig's <a href="http://twig.sensiolabs.org/doc/templates.html#comparisons" rel="nofollow">matches</a> operator to compare that segment to the page trigger. Note that there is a <a href="https://craftcms.com/docs/config-settings#pageTrigger" rel="nofollow">pageTrigger</a> setting, and this macro assumes you haven't changed that trigger.</p>

<pre><code>{% macro is_active(segment) %}
  {% set firstSeg = craft.request.getSegment(1) %}
  {% set secondSeg = craft.request.getSegment(2) %}
  {# is secondSeg a pagination segment? #}
  {% set isPagination = secondSeg and secondSeg matches '/^p[0-9]+/' %}

  {# active: firstSeg is our segment, and no secondSeg, or secondSeg is pagination seg #}
  {% if firstSeg == 'segment' and (not secondSeg or isPagination) %}
    active
  {% endif %}
{% endmacro %}
</code></pre>

<p>You might also want to use the <a href="https://github.com/marionnewlevant/craft-twig_perversion" rel="nofollow">twigperversion</a> plugin which provides a <code>{% return %}</code> tag:</p>

<pre><code>{% macro is_active(segment) %}
  ...
  {% return firstSeg == 'segment' and (not secondSeg or isPagination) %}
{% endmacro %}
</code></pre>

<p>and then <code>{{ macro.is_active('photography') ? 'active' }}</code></p>

CC BY-SA 3.0
2016-09-01T20:12:44.573Z
@AbbeyDesign is right: [getPageNum](https://craftcms.com/docs/templating/craft.request#getPageNum) is a cleaner way to go
Marion Newlevant
CC BY-SA 3.0
2016-09-01T20:10:40.330Z
2016-09-01T20:10:40.330Z
Marion Newlevant
<p>I'm using a macro to check segment 1 to set an active state on my navigation and all is working correctly. However I would like to change it so that if segment_2 is not empty it doesn't display the active state, with the exception of being pagination in segment-2</p>

<p>so <code>domain.com/photography</code> and <code>domain.com/photography/p2</code> would get active but <code>domain.com/photography/slug</code> would not be active</p>

<p>my current code is:</p>

<p>macro:</p>

<pre><code>{% macro is_active(segment) %}{% if craft.request.firstSegment == segment %}active{% endif %}{% endmacro %}
</code></pre>

<p>and nav:</p>

<pre><code>&lt;ul class="nav navbar-nav"&gt;
  &lt;li class="{{ macro.is_active('') }}"&gt;&lt;a href="/"&gt;Home&lt;/a&gt;&lt;/li&gt;
  &lt;li class="{{ macro.is_active('photography') }}"&gt;&lt;a href="/photography"&gt;All Photography&lt;/a&gt;&lt;/li&gt;
  &lt;li class="{{ macro.is_active('calendar') }}"&gt;&lt;a href="/calendar"&gt;Archives&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</code></pre>

<templating><navigation><macro>
active state macro how to check if segment 2 is empty
# active state macro how to check if segment 2 is empty

(Asked by: username_0 on 2016-09-01)

I'm using a macro to check segment 1 to set an active state on my navigation and all is working correctly. However I would like to change it so that if segment_2 is not empty it doesn't display the active state, with the exception of being pagination in segment-2
so domain.com/photography and domain.com/photography/p2 would get active but domain.com/photography/slug would not be active
my current code is:
macro:
{% macro is_active(segment) %}{% if craft.request.firstSegment == segment %}active{% endif %}{% endmacro %}

and nav:

  Home
  All Photography
  Archives

**Tags:** templating, navigation, macro


---

## 2 answers

### Answer by username_1 on 2016-09-01. Score: 2

Not tested, but I think you can test if it is a pagination page. So 
{% if (craft.request.getSegment('2') == '') or (craft.request.getPageNum() != 1) %}active{% endif %}


---

### Answer by username_2 on 2016-09-01. Score: 1

Here is a possible macro. It uses getSegment() to get the second segment, and twig's matches operator to compare that segment to the page trigger. Note that there is a pageTrigger setting, and this macro assumes you haven't changed that trigger.
{% macro is_active(segment) %}
  {% set firstSeg = craft.request.getSegment(1) %}
  {% set secondSeg = craft.request.getSegment(2) %}
  {# is secondSeg a pagination segment? #}
  {% set isPagination = secondSeg and secondSeg matches '/^p[0-9]+/' %}

  {# active: firstSeg is our segment, and no secondSeg, or secondSeg is pagination seg #}
  {% if firstSeg == 'segment' and (not secondSeg or isPagination) %}
    active
  {% endif %}
{% endmacro %}

You might also want to use the twigperversion plugin which provides a {% return %} tag:
{% macro is_active(segment) %}
  ...
  {% return firstSeg == 'segment' and (not secondSeg or isPagination) %}
{% endmacro %}

and then {{ macro.is_active('photography') ? 'active' }}

> **Comments:**
> **username_2**: @username_1 is right: [getPageNum](https://craftcms.com/docs/templating/craft.request#getPageNum) is a cleaner way to go  

---

