0

i will get tags from a specific parent id in each article

my Tags list :

. Id .... Name
+..1......parent
-..2......child1
-..3.......child2
+..4......parent2
-..5.......child1
-..6......child2

Post id 600 has 3 tags (ids 3, 5, and 6). I will show only tags with parent id 4 How can I do it???

1
  • Welcome to JSE. Please take our tour while you wait for support. If you have made any attempt to self-solve, please include your efforts as an edit of the question. Please continue to work on this issue yourself -- if you manage to resolve this problem yourself, please post an educational answer. – mickmackusa Sep 9 '20 at 21:21
1

There are a couple of ways you could do it, both involving examining the article tags which com_content produces, comparing them with the tags which you want to keep, and discarding the tags which don't match.

You can do this either by creating an override of the com_content layout display.php for displaying an article (as described in https://docs.joomla.org/J3.x:How_to_use_the_Template_Manager#Creating_Overrides) or you could use a content plugin which is triggered by onContentPrepare and remove the unwanted tags that way.

Either way, you should use the TagsHelper API to get the tags associated with the specific parent, and the searchTags() method will do this for you. Examples of calling this function are given in the Tags API Guide.

The basic plugin code which will do something like what you want is below. You may need to adapt it slightly - for example, this matches all descendants of tag with id=4, not just immediate children, and there are views other than com_content.article where articles are output. I'm not guaranteeing that it's totally bug-free!

<?php
defined( '_JEXEC' ) or die;
use Joomla\CMS\Helper\TagsHelper;
use Joomla\CMS\Factory;

class plgContentLimittags extends JPlugin
{
     public function onContentPrepare($context, &$article, &$params, $page = 0)
     {
        $parts = explode(".", $context);
        $app = Factory::getApplication();
        if ($app->isClient('site') && isset($parts[0]) && $parts[0] == "com_content" && isset($parts[1]) && $parts[1] == "article" && isset($article->id, $article->tags, $article->tags->itemTags))
        {
            $filters = array('parent_id' => 4);
            $allowedTags = TagsHelper::searchTags($filters);
            foreach ($article->tags->itemTags as $index => $tag)
            {
                $found = false;
                foreach ($allowedTags as $allowedTag)
                {
                    if ($tag->tag_id == $allowedTag->value)
                    {
                        $found = true;
                        break;
                    }
                }
                if (!$found)
                {
                    unset($article->tags->itemTags[$index]);
                }
            }
        }
        return true;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.