I fought a battle with EE on highlighting category based Navigation when on a single weblog:entry page. I got a solution working and would like to know if anyone has a better solution or comments. See attached image:
Problem:
The problem arises when I want to highlight the left hand dynamic navigation (list of weblog categories) on single a weblog:entry (article) page. Since any entry may belong to several categories there is no standard way.
Solution (elaborate):
- Get the category(ies) of the current entry (passed as an embed variable in my case) using something like:
current_cat="{exp:weblog:entries weblog="hazmat" limit="1" status="not home" disable="trackbacks|member_data|pagination"}{categories backspace='1'}{category_id},{/categories}{/exp:weblog:entries}"Then pass the string to javascript. The javascript will search for document.referrer (the last clicked link) for a category ( e.g. ...com/blah/etc/C127) and compare it with the current category. If it matches any of the categories it highlights that link. If not it will highlight the first category in current_cat.
{if "{embed:current_cat}"!=""}
{jQuery}
var current_cat="{embed:current_cat}";
$(document).ready(function(){
/*** Highlight menus when ***/
if(document.referrer.match(/\/C(\d+)\//)) {
matching=current_cat.match(RegExp.$1)[0]
$('.C'+matching).addClass('active')
} else {
singCat=parseInt(current_cat);
$('.C'+singCat).addClass('active');
}
});
{jscripts}
{/if}The actual highlighting is done by adding the class “active” to the matched link using jQuery (it is a bit more elaborate if not using jQuery but not much).
Is there a more EE compatible way of doing this?