i dunno…it seems that you switched requirements in there somewhere.
Frankly, I think you should break this down into separate functions.
Your first example had the toggle down fine…no need to change it.
Also, you have the ‘active_page’ link thing most of the way there…leave that too.
So the only thing you need to do is get the third function….open the sub-menu accordion if it contains an anchor with the active page class.
It shouldn’t be that hard.
Now, eventually, you might be able to combine no.2 and no.3 but that something for another day.