COMMUNITY FORUM
Related Items by Tag - Exclude Category
- Justin Thomas
-
Topic Author
- Offline
- Senior Member
Less
More
11 years 1 month ago #126387
by Justin Thomas
Related Items by Tag - Exclude Category was created by Justin Thomas
I would like to be able to exclude a category from the related items one of my K2 templates. It doesn't have to be dynamic as it will always be the same category so I can happily use cat ID.
Does anybody have any code they have used for this or can anybody show me how this can be done.
Many thanks
Does anybody have any code they have used for this or can anybody show me how this can be done.
Many thanks
Please Log in or Create an account to join the conversation.
- Lefteris
-
- Offline
- Moderator
Less
More
- Posts: 8743
11 years 1 month ago #126388
by Lefteris
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by Lefteris on topic Re: Related Items by Tag - Exclude Category
Hi. You need to hack K2 to do this which is not a good practice. The file that generates the related items query is components/com_k2/models/itemlist.php line 898.
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Kogos Grain
-
- Offline
- New Member
Less
More
- Posts: 15
9 years 2 months ago - 9 years 2 months ago #151194
by Kogos Grain
Replied by Kogos Grain on topic Related Items by Tag - Exclude Category
i know that's an old post, but i came up with a similar problem... i wanted to exclude a category from "related items (by tag)" in item view...
What i did is create an override and add a "continue" statement immediately after "foreach" in item.php.
For example, in item.php, around line 384,
find
and add "continue" based on category name, making it look like this:
Of course replace NAMEOFCATEGORY with your category name you want to exclude.
Now, the only problem i have is that if an item is excluded because it belongs to the "blacklisted" category, it's not replaced by another item. If i choose to show 6 related items, and one of them is excluded, i see 5... if 2 items are excluded, i see 4, etc...
Is there any way to make it always show the correct number of items, even if some are excluded, without hacking k2 core files???
Thanks in advance
What i did is create an override and add a "continue" statement immediately after "foreach" in item.php.
For example, in item.php, around line 384,
find
<?php if($this->item->params->get('itemRelated') && isset($this->relatedItems)): ?>
<!-- Related items by tag -->
<div class="itemRelated">
<h3><?php echo JText::_("K2_RELATED_ITEMS_BY_TAG"); ?></h3>
<ul>
<?php foreach($this->relatedItems as $key=>$item): ?>
<li class="<?php echo ($key%2) ? "odd" : "even"; ?>">
and add "continue" based on category name, making it look like this:
<?php if($this->item->params->get('itemRelated') && isset($this->relatedItems)): ?>
<!-- Related items by tag -->
<div class="itemRelated">
<h3><?php echo JText::_("K2_RELATED_ITEMS_BY_TAG"); ?></h3>
<ul>
<?php foreach($this->relatedItems as $key=>$item): ?>
[b]<?php if (($item->category->name) == 'NAMEOFCATEGORY') :
continue;
endif?>[/b]
<li class="<?php echo ($key%2) ? "odd" : "even"; ?>">
Of course replace NAMEOFCATEGORY with your category name you want to exclude.
Now, the only problem i have is that if an item is excluded because it belongs to the "blacklisted" category, it's not replaced by another item. If i choose to show 6 related items, and one of them is excluded, i see 5... if 2 items are excluded, i see 4, etc...
Is there any way to make it always show the correct number of items, even if some are excluded, without hacking k2 core files???
Thanks in advance
Last edit: 9 years 2 months ago by Kogos Grain.
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
Less
More
- Posts: 15920
9 years 2 months ago #151202
by Krikor Boghossian
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by Krikor Boghossian on topic Related Items by Tag - Exclude Category
If you don't want to (and you should not) hack K2's core you need a K2 plugin.
Alternatively there are several 3rd party extensions in the JED that offer this functionality.
Alternatively there are several 3rd party extensions in the JED that offer this functionality.
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.