Support community for TTG plugins and products.
NOTICE
The Turning Gate's Community has moved to a new home, at https://discourse.theturninggate.net.
This forum is now closed, and exists here as a read-only archive.
You are not logged in.
A couple of years ago I posted some code to highlight the current page's navigation menu item for CE3 and CE4 sites.
I recently got a question about how to make that work in Backlight. And while I may have eventually figured it out by myself, I didn't feel like going down that rabbit hole right now. So, naturally, I asked Matt.
See the update at the bottom of this post on my TTG-Tips site:
http://ttg-tips-and-tricks.barbeephoto. … -websites/
Thanks Matt!
Rod
Just a user with way too much time on his hands.
www.rodbarbee.com
ttg-tips.com, Backlight 2/3 test site
Offline
Hi Rod, I had a play with it with Backlight. This works to add the selected class to the current page menu item:
function ttg_scripts( $style, $path ) {
echo <<<SCRIPT
<script>
$(function() {
$("ul.menu a[href='" + window.location.pathname + "']").addClass("selected");
});
</script>
SCRIPT;
} // END
(or with alternative syntax:
function ttg_scripts( $style, $path ) { ?>
<script>
$(function() {
$("ul.menu a[href='" + window.location.pathname + "']").addClass("selected");
});
</script>
<?php } // END
)
That saves looping on the hrefs. Bear in mind that Backlight 1.1 uses short URLs for Backlight links, e.g. /contact rather than http://yoursite.com/contact. That's why window.location.pathname is used instead of window.location.href.
The following line adds 'selected' to the parent menu items as well:
$("ul.menu a[href='" + window.location.pathname + "']").addClass("selected").parents('li').children().addClass('selected');
Offline
And that's why Ben does the programming, rather than me. That's much cleaner than what I came up with. ;-)
Offline
You're too kind, Matt. It may help to know that I don't know how that last line actually works. My jQuery is rusty, especially when it comes to parent and child functions.
Offline
Cool.
Out of curiosity, by using
<<<SCRIPT
........
SCRIPT;
or the php closing and opening tags, are they're ways of avoiding having to escape the single quotes?
I'll update my blog post on this! thanks.
Rod
Just a user with way too much time on his hands.
www.rodbarbee.com
ttg-tips.com, Backlight 2/3 test site
Offline
They are. I have never user heredocs before (the SCRIPT syntax, where SCRIPT can be whatever you like). They're used for long strings where you can provide the content as-is without needing to escape quotes.
In the above example, it turned out to be cleaner to close and reopen the PHP tags, since the objective is to output the code there and then.
If you wanted to assign the content instead then heredocs would work well, e.g.
$content =<<<SCRIPT
........
SCRIPT;
Offline
Update: With the release of Backlight 1.2.3 you’ll find a sample PHPlugins script to highlight the current page's menu item in backlight/custom/phplugins/phplugins-pangolin-sample.php.
Rod
Just a user with way too much time on his hands.
www.rodbarbee.com
ttg-tips.com, Backlight 2/3 test site
Offline