Ok, this is easy, you can do this by using a php code to echo the class name into the current category only, for example the open tag of “li” will be like this:
<li <?php echo (get_query_var('cat') == $cat->term_id) ? 'class="current-menu-item"' : ''; ?> >
Hope this helps.
This should help me, but it doesn’t with my current code. Since I started this thread I’ve rewritten my code a bit. This is how it looks at the moment.
<?php foreach (get_categories() as $cat) : ?> <li class="item"> <a href="<?php echo get_category_link($cat->term_id); ?>"> <img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /> </a> <a class="link" href="<?php echo get_category_link($cat->term_id); ?>"> <?php echo $cat->cat_name; ?> </a> </li> <?php endforeach; ?>
As you can see I have a hyperlink with the class “link”. On the current category I’d like the class to be “link current-menu-item”, while all other links simply have the class “link”. How do I add your bit of code to solve this? Should it be inside the “a” tag or outside?
EDIT: I actually solved it by doing this;
<a class="link <?php echo (get_query_var('cat') == $cat->term_id) ? ' active' : ''; ?>" href="<?php echo get_category_link($cat->term_id); ?>"> <?php echo $cat->cat_name; ?> </a>
Thanks for the help!
I have a follow-up question! I’m posting in the same thread as it’s regarding the same code I have above. The question is simple, how do I exclude categories from my category list using my above code? That must be possible!
simply pass the $args you want to the function get_categories($args); and you can find examples in wordpress codex here
Ok, I tried it out but it still lists all my categories. I’m no PHP-guru so I’m not sure how to write it correctly. Here’s the code I tried;
<?php $args = array('exclude'=>'2'); foreach (get_categories($args) as $cat) : ?>
Am I close perhaps?
It was working after all, I just had to find all the correct ID’s for the categories. Thanks for helping!
- The topic ‘[Plugin: Categories Images] Set current menu item style to category links’ is closed to new replies.