WordPress.org

Forums

Categories Images
[resolved] Set current menu item style to category links (9 posts)

  1. Filespit
    Member
    Posted 2 years ago #

    Hello there. I'm using the Categories Images plugin on my website to, obviously, list my categories with images. It's working perfectly, however I'd like to set a "current-menu-item" style to the category that I'm currently browsing. How do I do that? If I add a style to
    "li" or "a" it will add it to every link in the list. Any suggestions?

    <?php foreach (get_categories() as $cat) : ?>
      <li>
        <img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
        <a href="<?php echo get_category_link(); ?>">
          <?php echo $cat->cat_name; ?>
        </a>
      </li>
    <?php endforeach; ?>

    http://wordpress.org/extend/plugins/categories-images/

  2. Muhammad
    Member
    Plugin Author

    Posted 2 years ago #

    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.

  3. Filespit
    Member
    Posted 2 years ago #

    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!

  4. Filespit
    Member
    Posted 2 years ago #

    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!

  5. Muhammad
    Member
    Plugin Author

    Posted 2 years ago #

    simply pass the $args you want to the function get_categories($args); and you can find examples in wordpress codex here

  6. Filespit
    Member
    Posted 2 years ago #

    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?

  7. Muhammad
    Member
    Plugin Author

    Posted 2 years ago #

    Yes your are and it should work fine

  8. Filespit
    Member
    Posted 2 years ago #

    It was working after all, I just had to find all the correct ID's for the categories. Thanks for helping!

  9. Muhammad
    Member
    Plugin Author

    Posted 2 years ago #

    You are always welcome :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic