How to get article intro Image by Id Joomla 3

2020-07-18 07:56发布

问题:

I'm trying to get Article's intro Image in Joomla 3.0 I found this code and it's not working:

$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 info that JRequest::getInt is depreciated and i when i'm trying to get $pictures i got null. Can somebody tell me how to get 1 intro image by article id ?

回答1:

Please 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!



回答2:

<?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 above code. It works for me.



标签: php joomla