简体   繁体   中英

Joomla 3: adding category-image in article template

I'm overriding the article template of my Joomla 3 website which is default.php . I need to add the category image into my article template. I already tried:

$db = &JFactory::getDBO(); 
$id = JRequest::getString('id'); 
$db->setQuery('SELECT #__categories.params FROM #__content, #__categories WHERE #__content.catid = #__categories.id AND #__content.id = '. $db->quote($id)); 
$category = $db->loadResult();
echo $category; 

The result is something like:

{"category_layout":"","image":"images\\/u14115.png"}

But how do I extract only the image from this JSON string?

You have to decode the string. Try PHP's json_decode. Add to your code:

Object:

$category = json_decode($category);
echo $category->image;

Array:

$category = json_decode($category, true);
echo $category['image'];

http://php.net/manual/en/function.json-decode.php

You can also do this natively in Joomla with something like this:

$category = JCategories::getInstance('Content')->get($id);
echo $category->getParams()->get('image');

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