Keyword

Related categories

  • Dominic Danson
  • Dominic Danson's Avatar Topic Author
  • Offline
  • New Member
More
9 years 7 months ago #140352 by Dominic Danson
Related categories was created by Dominic Danson
I'm trying to create a section within an Item where it will list all other items in the same category. I understand the Related Items By Tag function, but can't figure out how to get all items from the current category instead.

Can anyone help?

Thanks!

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
9 years 7 months ago #140371 by Krikor Boghossian
Replied by Krikor Boghossian on topic Related categories
Hello you will either need to publish a K2 content module inside the item.php file, or you need to search the JED or getk2.org/extend/extensions for a K2 plugin that extends the functionality of the related items.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

  • Dominic Danson
  • Dominic Danson's Avatar Topic Author
  • Offline
  • New Member
More
9 years 7 months ago #140389 by Dominic Danson
Replied by Dominic Danson on topic Related categories
Thanks.

I've managed to answer the problem by adding the following code to my item.php file:

<?php
// This code will display all items from current item category

$Id = JRequest::getInt('id');

$db = &JFactory::getDBO();
$queryItem = "SELECT catid FROM #__k2_items WHERE id = ".$Id;
$db->setQuery( $queryItem );
$result = $db->loadResult();

$query = "SELECT i.id, i.catid, i.alias, i.title, c.alias AS categoryalias".$fields;

$query .= "\n FROM #__k2_items AS i"
."\n INNER JOIN #__k2_categories c ON c.id = i.catid"
."\n WHERE i.published = 1 AND c.published = 1"
."\n AND (i.catid = $result)";

$db->setQuery( $query );
$items = $db->loadObjectList();

// Display items in Masonry object
if (count($items)) {
$catName = $this->item->category->name;
echo "<div class=\"col-12\">";
echo "<h3>More products from $catName</h3>";
echo "<div id=\"container\">";
foreach ($items as $item) {
$item->link = urldecode(JRoute::_(K2HelperRoute::getItemRoute($item->id.':'.urlencode($item->alias), $item->catid.':'.urlencode($item->categoryalias))));
echo "<div class=\"item col-3 related\">";
echo "<div class=\"item-inner\" >";
echo "<div class=\"item-image-wrapper\">";
echo "<img src=\"media/k2/items/cache/".md5("Image".$item->id)."_XL.jpg\" alt=\"$item->title\" />";
echo "</div>";
echo "<div class=\"item-details\">";
echo "<h2>$item->title</h2>";
echo "<a class=\"item-link\" href=\"$item->link\" title=\"$item->title\"></a>";
echo "</div></div></div>";
}
echo "</div></div>";
}

?>

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
9 years 7 months ago #140395 by Krikor Boghossian
Replied by Krikor Boghossian on topic Related categories
Nice :)
Remember to override the file so you won't lose any changes when you update K2.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum