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:
– Modules anywhere (paid version):

Modules anywhere code used in this example:

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

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

Part 2: How to add k2 related items in the sidebar as a module.

Be Sociable, Share!
Tarik Assagai
Tarik (@tarik916) is a web developer and owner of ACS Digital Media, a Sacramento based web development agency specializing in open source CMS systems. If you are looking for support for your current or upcoming projects, visit


  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 ?


    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.

  5. Hi is this also possible to use with tags? I would like to use related items by tag but as a module to post it somewhere else….


  6. Kutt Niinepuu /

    Good job! Thank you!
    I only wonder if it is possible to use a specific category name instead of the ID in the parameter override: something like { module Related by category|category_name=some_category_name }?

    Problem is, I have the items I wish to relate to this article in another category. The category has the same NAME as the title of the current article, so if there were a parameter to accept a category name, I could feed it with the $this->item->title; or something.

    • tarik916 /

      The module only works by the ID since it is the best way to track an item. The name can be changed, but the ID always remains the same.

  7. Is there anyway this can be set to work in a module positions. rather than at the bottom of the article.

    • tarik916 /

      This code would not work that way. Since this feature is nto built into the K2 module, it would take some custom PHP to make it work. You would have to match the article ID within the module.

  8. Vincent F. /

    Hi Tarik,

    does it works with joomla 2.5.6 and K2 2.5.7 ?
    Because I copy/past module content files but nothing appears for me ???


    Vincent F.

    • Vincent F. /

      Yep ! I undrstand the problem.
      We need to use now pro version for mudole parameter !

      • tarik916 /

        Good tip! I did not realize that as the old free version allowed parameters. Thanks!

    • tarik916 /

      Hi Vincent,

      This should definitely work for all versions. Were you able to figure it out?

  9. Awesome! Mine is showing articles from all categories, not just the ones from the articles default category. Is there a way to change this?

    Thanks so much!

    • tarik916 /

      Hi Liz,
      The latest version of modules anywhere requires the paid version in order to access module parameters. This is most likely your issue.

  10. Thanks for the wonderful tutorial.

    Is it possible to include only one article. The purpose is, I want to show the author name, avatar and item tags from the K2_content module in K2 tag.php file, which is still missing in K2 component. As per your tutorial, the category_id is already getting the right id. I have generated the item id in tag.php file. Therefore, my desired code to use:

    {module Related by category|category_id=category->id; ?>|itemIncludeOnly=id); ?>}


    • Tarik Assagai /

      Sorry for the late reply. To do this, you could specify the number of results in the module, or override the parameter in the embed code.

  11. There is another way to do the same thing.
    Just use “K2 content” module and hide everything apart from the title.
    It worked like a charm for me.

    But thanks to keep me aware of some different workaround.


  12. Hi Tarik And thanks for this tutorial
    i insert the code “{module Related by category|category_id=item->category->id; ?>|itemExclude=item->id; ?>}” in item.php
    but the current item still displaying, i use j3.x and k2 2.6.7
    and module any where v3.2.4 and template Gavick news, is there any modification should i do

    • Tarik Assagai /

      I have not tried this with 2.6.7, but the module parameters should be the same. If your PHP code is written properly (the comment may have stripped your tags) and you are using the pro version of Modules Anywhere, it should work properly.

  13. Hi !
    Great tuto, but I have a question, I work on joomla 3.1 and I cannot seleced the item category_id by code “category_id=category->id;”

    It doesn’t work and the module display all of elements of my website. Perhaps joomla 3.1 has not the same syntax to add the category ID ?
    Can you help me ?
    Thanks a lot !


    • Tarik Assagai /

      Sorry for the late response. Spam comments cause me to miss the real ones. This sounds like an issue with Modules Anywhere. Do you have the pro version?


  1. How to add K2 related items by category in the sidebar (module) | Slurp it up! I'm talking about the web. - [...] is a follow-up to my previous post on adding related k2 items by category. If you have not yet …
  2. How to add K2 related items by category as a module | Slurp it up! I'm talking about the web. - [...] is a follow-up to my previous post on adding related k2 items by category. If you have not yet …

Leave a Reply