How to get the tags associated to an article in Joomla

时光总嘲笑我的痴心妄想 提交于 2019-12-10 13:01:34

问题


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();

回答1:


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




回答2:


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);



回答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>';



回答4:


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);


来源:https://stackoverflow.com/questions/19215132/how-to-get-the-tags-associated-to-an-article-in-joomla

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!