I need to show the latest 3 content items from the database.

I want to show the:

  • Intro Image
  • Article title (with a link to the article)
  • Published Date
  • Intro Copy (limited to 100 characters)

I have written the following, which is working but I need to json_decode the images in order to show the introimage:

<div class="row" id="interested-in">
    <div class="col-12">

        <h2>Heading Here</h2>
        <div class="row">


            use Joomla\CMS\Language\Text;
            use Joomla\CMS\Date\Date;
            use Joomla\CMS\Factory;

            $db = JFactory::getDbo();

            $query = $db->getQuery(true);

            $query->order('id DESC ');

            $res = $db->loadObjectList();

            $i = 0;
            foreach ($res as $r) {
                $introimage = $r->images;
                $id = $r->id;
                $title = $r->title;
                $published = $r->publish_up;
                $intro = $r->introtext;
                if (++$i > 3) break;

                echo '<div class="col-lg-4 col-md-6">';
                echo '<div class="card">';
                if ($introimage == null || $introimage == '/') {
                    echo '<div class="card-img-top holding-image" alt="' . $title . '"></div>';
                } else {
                    echo '<img class="card-img-top" src="' . $introimage . '" alt="' . $r->title . '"/>';
                echo '<div class="card-body">';
                echo '<a href="' . JRoute::_('index.php?option=com_content&view=article&id=' . $id) . '">';
                echo '<h5 class="card-title mb-4">'.$title.'</h5>';
                echo '</a>';
                echo '<time datetime="'. $published . '" class="mb-2 text-muted">'.$published.'</time>';
                echo '<p class="card-text">'.mb_strimwidth("$intro", 0, 80, "...").'</p>';
                echo '</div>';
                echo '</div>';
                echo '</div>';
            } ?>

I also need to echo the date in a different format, I've tried

echo '<time datetime="'. $published . '" class="mb-2 text-muted">'.$published->format(Text::_('DATE_FORMAT_LC')).'</time>';

But that gives me an error:

Call to a member function format() on string

  • Why have the $i counter and conditional break if your sql ensures that not more than 3 rows are returned? Commented Jul 15, 2021 at 10:51

1 Answer 1


Resolved this as follows:

In foreach, for the intro image, I replaced

$introimage = $r->images;


$introimage = json_decode($r->images)->image_intro;

For the date formatting, I added:

use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;

and replaced

echo '<time datetime="' . $published . '" class="mb-2 text-muted">' . $published.'</time>';


echo '<time datetime="' . $published . '" class="mb-2 text-muted">' . HtmlHelper::date($published, Text::_('DATE_FORMAT_LC')) . '</time>';
  • You might not be into this style, but perhaps you would entertain using string templating with printf() and sprintf() to avoid lots of interpolation and concatenation and echoing. 3v4l.org/1po1G Commented Jul 15, 2021 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.