Forum

Thread tagged as: Question

Show active link in nav list of galleries

I am using Perch Gallery and I have a list of all galleries in the nav bar (which shows up on every gallery page). I want to add a class to the active link.

Nav bar on album.php looks like this:

  <?php perch_gallery_albums(array(
      'template' => 'album-link.html',
  )); ?>

Album-link.html looks like this:

<a href="https://[website].com/<perch:gallery id="albumSlug" />">
    <perch:gallery id="albumTitle" />
</a>

So I should add an if statement that goes something like this:

<a 
<perch:if match="eq" value="albumSlug"> class="active"</perch:if>
href="https://[website].com/<perch:gallery id="albumSlug" />">
    <perch:gallery id="albumTitle" />
</a>

But I just don't know how to say "if the albumSlug of the album on this page is equal to the albumSlug in the array"

S Tasker

S Tasker 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You'll need to pass the current slug into the template. Something like:

PerchSystem::set_var('current_slug', perch_get('slug'));

and then in your template

<perch:if id="albumSlug" match="eq" value="{current_slug}"> class="active"</perch:if>

I seem to be entering the first piece of code wrong ... I've tried putting it in both album.php and album-link.html templates a few lines above the (updated) if statement like this:

 <?php 
    PerchSystem::set_var('current_slug', perch_get('slug'));
?>

I notice the live version seems to comment-out this line when it's in album-link.html and it doesn't show up at all in album.php

Drew McLellan

Drew McLellan 2638 points
Perch Support

It needs to go before you display the template where the perch:if test happens. You also need to replace perch_get('slug') with whatever you need to pull in your album slug.

So I have put it in album-link.html above the if statement (this means that it would be repeated in the array) ... but when I view the page I can see that the live version still comments it out in the HTML?

I also need help working out what I need to pull in my album slug. At the moment the only thing I can think of to try is this, but I don't think it'll work for me:

<?php 
    PerchSystem::set_var('current_slug', perch_get('albumSlug'));
?>

I have thought of a sort of manual work-around - as my client will need some of her galleries to be "invisible" (not linked to in the main nav) I have decided it'll be easier to make the nav a general content type and get her to manually define the galleries that she wants to show up there. I should be able to find some general PHP code that will help me match the URL to the slug and define the active link. Thanks anyway!

I took a different approach to the same situation. In my case, I wanted to highlight the active link, but have the option of having a page with my navigation but no particular link highlighted.

Searching, I stumbled on this 2005 24 Ways article by one Drew McLellan titled Auto-Selecting Navigation.

As the article directed, in addition to the link on the nav item one needs to add a class to the body tag, which is described in the Perch documentation:

Add a class to the body tag

The select options in the body_attributes.html template in content —> pages —> attributes can use allowempty="true" to allow no class, and then the other classes are targeted- "activated"- via your CSS.

The body class is then chosen in the details section of any given page.

I take this opportunity to ask the 2017 Drew McLellan to extend my thanks to the 2005 version.