How to get article intro Image by Id Joomla 3
I'm trying to get an intro Image article in Joomla 3.0 I found this code and it doesn't work:
$article = JTable::getInstance("content");
$article->load(JRequest::getInt("id")); // Get Article ID
$article_images = $article->get("images"); // Get image parameters
$pictures = json_decode($article_images); // Split the parameters apart
// Print the image
echo "<img src='" . $pictures->{'image_intro'} . "' alt='" . $pictures->{'image_intro_alt'} . "'>";
I got information about what is being JRequest::getInt
depreciated and I $pictures
got null when I try to get it . Can someone tell me how to get 1 intro image by article id?
+4
woj_jas
source
to share
2 answers
Try with this code:
$article_id = JFactory::getApplication()->input->get('id'); // get article id
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('images'))
->from($db->quoteName('#__content'))
->where('id = '. $db->Quote($article_id));
$db->setQuery($query);
$result = $db->loadResult();
$intro_image = json_decode($result)->image_intro;
echo $intro_image;
Good luck!
+3
emmanuel
source
to share
<?php foreach ($this->items as $i => $article) : ?>
<?php $image = json_decode($article->images,true)['image_intro']; ?>
<?php if(!empty($image)){ ?>
<img src="<?=$image?>" alt="">
<?php } ?>
<?php endforeach; ?>
Use the above code. I like.
0
Hossein shafiei
source
to share