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 (paid version): 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; ?>}
Part 2: How to add k2 related items in the sidebar as a module.
24 Comments
Trackbacks/Pingbacks
- 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 ...
- 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 ...

Thanks, great tutorial, again
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);
Sorry, I don’t know how to do that.
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.
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.
Thanks man ! greats works !
I test it on the fly ..
Keep up the works
Cheers !
Vincent F.
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….
Thanks!
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.
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.
Is there anyway this can be set to work in a module positions. rather than at the bottom of the article.
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.
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 ???
Regards,
Vincent F.
Yep ! I undrstand the problem.
We need to use now pro version for mudole parameter !
Good tip! I did not realize that as the old free version allowed parameters. Thanks!
Hi Vincent,
This should definitely work for all versions. Were you able to figure it out?
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!
Hi Liz,
The latest version of modules anywhere requires the paid version in order to access module parameters. This is most likely your issue.