Tam N. Tam N. - 9 months ago 52
jQuery Question

jquery lavaLamp not working for Wordpress single post

Short question: how do you use JS/jquery to return index of an element within some , given its class name?

Long question: I've been at this problem for a few hours and I can't seem to find a solution. I have a Wordpress site, which uses a theme that utilizes superfish and lavalamp jquery. the lavalamp works fine for categories and pages, but for a single post, the lavalamp keeps going to the first item of the list.

It's worth noting that I have done some serious googling around, but the answers are either too old, or they don't work for my case. I have jQuery 1.7.1, lavaLamp 1.3.3 (updating to 1.3.5 breaks everything), and superfish 1.4.8.

I've narrowed it down that the script that calls the lavaLamp plugin isn't returning the correct index of the current category for a single post. Here's what it looks like:

var active_subpage = jQuery('ul.sf-menu ul li.current-cat, ul.sf-menu ul li.current_page_item').parents('li.top-level').prevAll().length;
var isHome = <?php if (is_front_page() || is_home()) echo('1'); else echo('0'); ?>;

if (active_subpage) jQuery('ul.sf-menu').lavaLamp({ startItem: active_subpage });
else if (isHome === 1) jQuery('ul.sf-menu').lavaLamp({ startItem: 0 });
else jQuery('ul.sf-menu').lavaLamp();

How do I modify the script so that it returns the index of the "current-cat" for that post?

I've pasted what I think you'd need HERE. You can take a look at the live site HERE.

Thank you very much in advance!

Answer Source

Your active_subpage query only works for elements that are selectable from a submenu, the first selector doesn't seem like it will match anything, because its looking for the .current-cat in a sub-menu, when it is actually a top menu.
I think it will work if you remove the second <ul> in the first selector of the active_page query.
I see the problem, the li.current-cat is the parents('li.top-level') that we're looking for.
The two queries may need separating.
Or change the first selector to include a descendant (<a>) so that parents('li.top-level') would make a match