简体   繁体   中英

How to get the tags associated to an article in Joomla

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

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

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

只是为了添加Marko D的答案,添加它来格式化文章/博客布局中的标签。

echo JLayoutHelper::render('joomla.content.tags', $tags->itemTags);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM