morktron
morktron

Reputation: 915

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? ;)

Upvotes: 1

Views: 930

Answers (2)

Akirat
Akirat

Reputation: 41

The code above did not work for me in Joomla 3.4.1 and K2 2.6.8. Here is a solution that will work with any version (J1.5/J2.5/3.0 and any version of K2).

<li class="<?php        
        $active_sub = intval(JRequest::getCmd('id'));

        if ($item->id == $active_sub) echo 'active';
        ?>">

Upvotes: 0

Erik R&#252;hling
Erik R&#252;hling

Reputation: 56

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.

Upvotes: 2

Related Questions