Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm coding a graphic menu that consist in five divs with a background each. On mouseover the background changes and the child div disappears. On click, each one works like a link to the correspondent category. I want to apply some changes to the clicked div, so I was wonder if there is some function in wordpress to get the selected category. So I can check for It and in function of wich category is selected apply the changes to the correspondent div.

Thanks in advance.

share|improve this question
2  
Without actual code, it will be nearly impossible for us to provide suggestions. (Note: please post the PHP code, not the parsed/output HTML. –  Chip Bennett Jan 27 '12 at 15:54
add comment

2 Answers

up vote 1 down vote accepted

If you use wp_list_categories (see Codex), it automatically adds the classes cat-item,cat-item-{ID} and (to the current category being viewed) current-cat. (It also adds current-cat-parent to the parent of the current category).

This should be enough to style any particular category a certain way, and style the current category a particlar way.

However, you can obtain the current category being viewed by, in your category template file, using:

$term = get_queried_object();

The current category's slug is given by $term->slug. This would allow you to add a certain styling to the current category (the exact details of how to do that would depend on how you were displaying the category links).

share|improve this answer
    
This was really helpfull. Thanks for your answer. I'm not using the wp_list_category, because I'm doing this menu from scratch using divs with background images so the get_queried_object() its going to be the more suitable. –  limoragni Jan 27 '12 at 19:05
add comment

I think you are looking for the in_category condition.

http://codex.wordpress.org/Function_Reference/in_category

With this you can check if something is in a certain category and execute script, and if it's in another category do something different.

<?php 
if ( in_category( 'category1' )) {
    // DO SOMETHING HERE
} elseif ( in_category( array( 'Tropical Birds', 'small-mammals' ) )) {
    // DO SOMETHING ELSE
} else {
   // DEFAULT ACTION IF NOT IN ANY SPECIFIED CATEGORY
}
?>
share|improve this answer
    
This will check if the current post is in a particular category. Thus it may return true for category terms other than the one being viewed because the post exists in both terms. –  Stephen Harris Jan 27 '12 at 16:08
    
Good point Stephen. Didn't think of that. –  Jason Bahl Jan 28 '12 at 16:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.