phpjoomla2.5joomla-k2

How to add an active class to a Joomla K2 content module


I'm trying to add a class to the active <li> in the Joomla K2 Content module to make a dynamically created menu. So far I have got:

<li class="<?php if ($item->id == $active_id) echo 'active';?> hello">

I put a hello in there to check the template override is working and yes it is.

I obviously don't know php, I assume this is an easy question for someone that knows Joomla K2 and php? ;)


Solution

  • I had the same problem and came up with this solution (Joomla 3.0.3 & K2 2.6.5)

        <li class="<?php
    
        $active_sub = substr(($app->input->getCmd('task', '')), 0, 3);
    
        if ($item->id == $active_sub) echo 'active';
                                                       ?>" >
    

    'task' is defined in the main index.php and returns the submenu itemid along with the menu item name, use substr to get the first 3 characters which correspond to $item->id in the K2 module. This solution will work until you get more than 999 menu items in the module.

    I'm not a PHP expert either, but this works well for my template.