How to get the tags associated to an article in Joomla
Asked Answered
F

4

8

I need to get the TAGS associated with an article in Joomla 3.1.5

I have tried the following but they do not return a string:

echo $article->item->tags->itemTags;

and

$tags = $article->get("tags");

And just for the record I am loading the article info as such (getting the article title works perfectly)

$article = JTable::getInstance("content");
$article->load(JRequest::getInt("id"));
$pageTitle = $article->get("title");
$user =& JFactory::getUser();
Fourposter answered 6/10, 2013 at 23:25 Comment(0)
I
10

If you look in components/com_content/views/article/tmpl/default.php, the tags are being displayed like so:

if ($this->params->get('show_tags', 1) && !empty($this->item->tags->itemTags)) {
    $this->item->tagLayout = new JLayoutFile('joomla.content.tags');
    echo $this->item->tagLayout->render($this->item->tags->itemTags);
}

So you can base it on this:

Hope it helps

Iredale answered 7/10, 2013 at 12:41 Comment(0)
F
16

If you want to load article tags in a module/plugin etc, and assuming $id is the id of the article, you can do

$tags = new JHelperTags;
$tags->getItemTags('com_content.article', $id);
var_dump($tags);
Folliculin answered 22/1, 2014 at 23:49 Comment(0)
I
10

If you look in components/com_content/views/article/tmpl/default.php, the tags are being displayed like so:

if ($this->params->get('show_tags', 1) && !empty($this->item->tags->itemTags)) {
    $this->item->tagLayout = new JLayoutFile('joomla.content.tags');
    echo $this->item->tagLayout->render($this->item->tags->itemTags);
}

So you can base it on this:

Hope it helps

Iredale answered 7/10, 2013 at 12:41 Comment(0)
S
3

Rendering article tags in a module that displays Joomla articles, such as mod_articles_latest.

$itemtags = (new JHelperTags)->getItemTags('com_content.article', $item->id);
$taglayout = new JLayoutFile('joomla.content.tags');
$tags='';
if( !empty($itemtags) )
    $tags = '<div class="itemtags">'.str_replace(',','',$taglayout->render($itemtags)).'</div>';
Sculptor answered 22/1, 2018 at 22:7 Comment(5)
Hi, I've tryed this suggestion in my mod_articles_latest override without success. No tags are displayed. Can you help me?Mimicry
Did you print the $tags constant in default.php eg: <?php echo $tags; ?>Sculptor
What a shame!!! I forgot it!!! :( Thanks so much Nadal for your suggestion. I would like also to display into a different way those tags. The actual html structure for rendering tags is as "ul" and "li". Where should I work to edit this structure with an override? Could you please suggest me?Mimicry
Ok, I can override /layouts/joomla/content/tags.php but this way I'll overryde also the layout for articles meanwhile I would have a specific layout only for the module. Any suggestion about this?Mimicry
If you remove the list element, you'll lose the tag's numerical value used for the class. What is the desired output? You can achieve any display by manipulating CSS, without removing the <li>. If you must remove the list element, use preg_replace instead of str_replace and write the regex format to strip all but the anchor. Test your expression format at linkSculptor
I
2

Just to add to Marko D's answer, add this to format the tags like in an article/blog layout.

echo JLayoutHelper::render('joomla.content.tags', $tags->itemTags);
Immotile answered 23/11, 2017 at 3:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.