How to add related items by category in K2 (Joomla)

The latest version of K2 has a built in option to allow you to display related items by tag at the bottom of your K2 item. In my experience, most customers don’t want to bother with tags, and would rather have an automated list of related items by category. I’m sure this would be a simple update to K2, but until this feature is added, you can use this method. This tutorial will show you how to use Modules Anywhere and K2 template overrides to create a “related item by category” list within your K2 article.

Require components/plug-ins:
- K2 of course: http://getk2.org/
- Modules anywhere: http://www.nonumber.nl/extensions/modulesanywhere

Modules anywhere code used in this example:

{module Related by category|category_id=<?php echo $this->item->category->id; ?>}

UPDATE!
This feature will still display the current item in the related by category list. In order to solve this, I have updated two files in the mod_k2_content module folder (version 2.5) by adding a new “exclusion” parameter. You can download the files here. Replace the files in /modules/mod_k2_content/.

With this code in place, the new modules anywhere parameter is:

{module Related by category|category_id=<?php echo $this->item->category->id; ?>|itemExclude=<?php echo $this->item->id; ?>}

9 Comments

  1. Mihail /

    Thanks, great tutorial, again :)

  2. Mihail /

    Hi Tarik,
    do you know another way to override module parameter without using 3rd party component/module like “Modules Anywhare”?

    Just plain php code like:
    $document = &JFactory::getDocument();
    $renderer = $document->loadRenderer(‘modules’);
    $options = array(‘style’ => ‘xhtml’);
    $position = ‘User5′;
    echo $renderer->render($position, $options, null);

  3. Hi, thanks for this great tips ! very useful !
    I want to know if it’s possible to add a part of code to remove current article from related list please? Is it possible ?

    Regards,

    Vincent F.

    • tarik916 /

      Great idea! With the current release of mod_k2_content, this is not possible, but I have updated the post (at the bottom) with a link to modified mod_k2_content files to enable this feature. Hopefully this will be added to future versions.

  4. Vincent F. /

    Thanks man ! greats works !
    I test it on the fly ..
    Keep up the works
    Cheers !

    Vincent F.

Leave a Reply