Highlighting the current page’s navigation menu item in TTG websites

regular menuFor those using the TTG Lightroom Web Gallery plug-ins, here’s a quick and easy way to highlight the navigation link of the page that is currently being viewed. As is, the plug-ins let you set colors for text and background for the link in both normal and hovered states. That is, the link looks one way when simply looking at it and another way when you hover your mouse over it. You’ve all seen this.
Some people also like to have the current page’s link in the navigation menu to be highlighted so that their visitors can easily tell what page they’re viewing. But there’s no way within the plug-ins to change the look of a link based on what page is being viewed. However, with just a little bit of custom css and a short jquery script, you can add this functionality.

First of all, you’ll need to enable phplugins. Do this for all TTG pages, galleries, indexes, etc. The documentation on how to do that is here. Be sure to set up for global (site-wide) use.

If you’re using Publisher, you’ll only need to update, export and upload your publisher gallery and autoindex templates. If you’ve got an extensive site with lots of galleries or other pages that are not Publisher managed, you’ll need to update, export, and upload all of these individually. As far as the uploading part, you can get away with uploading just the index and mobile files (index.php and mobile.php). You may or may not have a mobile.php file; that depends on plug-in settings.
Note: if you also have index.html files left over from a previous export, you can safely delete those. Use of phplugins requires that your pages be exported as php files. The html files are no longer used.

Once you have phplugins enabled for all your pages, you’ll need to activate the Custom css section of your global phplugins.php file. That documentation is here.

Believe it or not, that was the hard part.

Now you’re set up so that all of your pages will be receiving the same custom css file. We’ll get to adding code to that in a minute, but first, let’s set up the jquery script that will make all of this work. Here’s the script. I found it with just a short Google search and have modified it for the TTG page structure:

<script>
$(document).ready(function() {
$("#navigation li a").each(function() {
if ($(this).prop("href") == window.location.href) {
$(this).addClass("selected");
}
});
});
</script>

So what does this do? The first line after the script tag simply tells the script to wait to execute until the page is loaded. That’s pretty standard stuff from what I’ve been learning (hey, I’m kinda new to this).
The next line tells the function to look at the page and select links (the ‘a’) in a list (li) that are inside the navigation div (#navigation).
The script then looks to see if any of those links match the URL of the current page. If any of them do, it then adds the css class of “selected” to that <a> tag (the link).
That’s all that does. It just adds the “selected” class to the menu link of the current page.

So now we need to get that script into all of our pages. A good place to do that is right after all the other scripts have loaded in the head section of the page. To do this, we use the ttg_head_end phplugins hook in your phplugins.php file. But look, we’re already using that to activate the custom css. Not a problem. Put your mouse cursor at the end of this line and hit enter:

<link href="/phplugins/css/custom.css" rel="stylesheet" />

Now copy the above script (including the script tags and paste it right after the above line and save the file.

This part of your phplugins.php file should now look like this:

function ttg_head_end( $style, $path ) {
echo '
<link rel="stylesheet" href="/phplugins/css/custom.css" />

<script>
$(document).ready(function() {
$("#navigation li a").each(function() {
if ($(this).prop("href") == window.location.href) {
$(this).addClass("selected");
}
});
});
</script>
';
} // END

So now all of your pages will add the “selected” class to the menu link of the current page, if it exists. Visually, this by itself does nothing. Next we’ll create the “selected” css class and add some styles.
Using a plain text editor, open the custom.css file you’ll find inside the phplugins/css/ folder.
Add this code:

.selected {
background-color: #2b2b2b !important;
color: white !important;
}

This adds a dark, nearly black background to the menu link and changes the color of the text to white. I’ve included the !important declaration so that other, more specific rules won’t override the styles.
Set the colors to whatever you like or add borders or any other styles you want.

Now be sure to upload the phplugins.php and custom.css files to their proper location on your server.

mobile-menuNote that when visiting one of your gallery pages on the desktop, you won’t see any highlighted menu links. That’s because there is no menu item for individual galleries. You will, however, see the highlighting in the mobile menu. So be sure to resize your browser and see what happens when the navigation collapses into the mobile menu. Depending on other colors you may have set (like the Collapsed Menu background-color), you may run into clashing or matching colors).

drop-downIf you’re using the drop-down navigation feature of TTG (also requires the use of phplugins) you’ll see that the drop-down items will be affected by the script and will take on the same styling.

3 thoughts on “Highlighting the current page’s navigation menu item in TTG websites”

  1. Hi Rod,

    thanks for the hint, it works well!

    The only problem I have is that for any subpage, the menu item will not be highlighted any more. I use TTG galleries and as soon as I click on a gallery, the gallerie thumb page shows and the menu item does not show as selected anymore. Same on the return pages of the form, when send.

    I am sure there is a way to also incorporate code to make it work on those pages.

    Cheers

    Peter

    1. that’s because there is no menu item for individual galleries or the Contact form’s acknowledgment page. You’d have to come up with code that would also dynamically create the menu items (and then remove that item later)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Newsletter

Take a look through the Newsletter archives or subscribe.

Keep up to date on workshop offerings, print sales, photo tips, puppy pictures and more!



Affiliate links below! If you buy after clicking these links, I get a taste. Win-win!

Maven Filters


High quality magnetic photo filters that will change your photographic life!
Affiliate link.


Topaz Labs

Awesome plug-ins to reduce noise, sharpen, and resize images. Affiliate link!


Think Tank

Affiliate link!

Lightroom Classic The Missing FAQ

Highly recommended. This is my go-to Lightroom book. Click on the book image. Affiliate link!