views:

121

answers:

2

I have the following Nav:

<li id="categories">
    <ul>
        <li class="cat-item cat-item-8 current-cat"><a href="#">Link</a>
            <ul>
                <li class="cat-item"><a href="#">Link</a></li>
                <li class="cat-item"><a href="#">Link</a></li>
                <li class="cat-item"><a href="#">Link</a></li>
                <li class="cat-item"><a href="#">Link</a></li>
            </ul>
        </li>
        <li class="cat-item cat-item-10"><a href="#">Link</a>
            <ul>
                <li class="cat-item"><a href="#">Link</a></li>
                <li class="cat-item"><a href="#">Link</a></li>
                <li class="cat-item"><a href="#">Link</a></li>
                <li class="cat-item"><a href="#">Link</a>
                    <ul>
                        <li class="cat-item"><a href="#">Link</a></li>
                        <li class="cat-item"><a href="#">Link</a></li>
                        <li class="cat-item"><a href="#">Link</a></li>
                        <li class="cat-item"><a href="#">Link</a></li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
</li>

and the following JS:

jQuery("#categories li.cat-item").each(function(){
    var item = jQuery("<span>").addClass('plus'),
        that = jQuery(this);

    if ( that.has("ul").length ) {   
        item.click(function(e){
            var self = jQuery(this);
            self.text( self.text() === "+" ? "-" : "+" )
                .parent().next().toggle();
            e.preventDefault();
        }).text('+');

        that.find(".children").hide();
    }

    that.children("a").prepend( item );
});

This builds a nice toggle menu for my categories.

However what I want it to do is based on what category I am currently viewing show the corresponding menu to be opened when the user lands on the page. This needs to work for both posts and categories.

Thanks.

A: 

Just put this after your code:

jQuery('#categories .current-cat span.plus:first').click();

which simulates a click on the first plus sign in the current category.

If you want to have the whole tree to be expanded, just omit the :first:

jQuery('#categories .current-cat span.plus').click();

Btw the menu did not collapse as I tried it, I had to change your code to that.children("ul").hide();

Update:

If .current-cat is on a li element in a subtree, you can do this:

jQuery('#categories .current-cat').parents('li:not(#categories)')
                                  .andSelf()
                                  .find('span.plus:first').click();

This finds every parent li, includes the current li (andSelf(), in case it is the again a root of a subtree) and clicks the first + sign in each of these.

Felix Kling
How can I get this to work for deeper navigation so for example: /category/cat1/subcat1/ and I'm on subcat1. And I also need it to work for posts. Thanks.
Cameron
@Cameron: I updated my answer. As I already said in one of my comments I don't know what you are aiming at with "posts". I don't even know that it does not work. You have to give way more information on that. I should not be too difficult for you to adjust the selectors though.
Felix Kling
What I'm wanting to is say I am viewing a post within a category then that category will be open in the list.
Cameron
@Cameron: Well I don't know about the internals of Wordpress but if you can identify the category you can just use a selector `jQuery('#categories .cuategory-name')` and then the rest of the code.
Felix Kling
A: 

Have Wordpress insert the category name into an ID on a parent container (say the BODY tag) and then write the CSS for each category/ID pair that opens the corresponding menu.

Todd