5

NOTE: The categories are not the categories which are related to product those categories are and extension categories like for example this categories belong to blog and there are some items under this categories.

I have some categories which are fetched dynamically in this way

<li><a href="<?php echo $cat->getAddress(); ?>" ><?php echo $cat->getTitle(); ?></a></li>

Now if a category is selected I want to show it highlighted in this way means it should be shown as in active stage. enter image description here

so far I tried giving class="current" to <li> and by writing CSS for that but that doesn't worked please help me in solving this thanks in advance.

Please someone look into this issue.

4 Answers 4

1
+50

Assuming the categories fetched dynamically using foreach loop.

<?php foreach ($cats as $cat): ?>
   <?php $cat_u = $cat->getAddress(); ?>
   <?php $cur_u = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>
   <li class="<?php if($cat_u == $cur_u) echo "active" ?>"><a href="<?php echo $cat->getAddress(); ?>" ><?php echo $cat->getTitle(); ?></a></li>
<?php endforeach; ?>


<?php $cat_u = $cat->getAddress(); ?>-->This thing gets the address of your categories

<?php $cur_u = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>-->This will fetch your present pages URI.

and <li class="<?php if($cat_u == $cur_u) echo "active" ?>">

if categories address=Current uri then that <li> class will be active.

Hope this works for you

0
1

I think you can check it by:

<li<?php echo in_array($cat->getId(), Mage::registry('current_category')->getPathIds()) ? ' class="current"' : '' ?>>

Hope it helps, good luck! ;)

2
  • thanks for the answer but this thing crashing my page
    – Abdul
    Dec 17, 2014 at 8:54
  • can you check if you have "Mage::registry('current_category') initialized ? If you do, try to replace ->getPathIds() with getId().
    – dsitovs
    Dec 17, 2014 at 10:04
0

Add Following Jquery code to your navigation file.

<script>
$j(function() {
  $j('.list-group a[href^="/' + location.pathname.split("/")[2] + '"]').addClass('current');
});
</script>

In 3rd line of above code just change .list-group to your class(class of navigation division.)

Good Luck!!!

1
  • Doesn't make any sense anyway thanks for the answer
    – Abdul
    Dec 17, 2014 at 9:06
0

You can try with below code. It's works for me.

<?php $current_cat = Mage::registry('current_category'); $selectclass = ($current_cat->getId() == $cat->getId())?' class="current"':'' ?> <li<?php echo $selectclass;?>><a href="<?php echo $cat->getAddress(); ?>" ><?php echo $cat->getTitle(); ?></a></li>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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