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">
<?php
use Joomla\CMS\Language\Text;
use Joomla\CMS\Date\Date;
use Joomla\CMS\Factory;
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__content');
$query->order('id DESC ');
$query->limit('3');
$db->setQuery((string)$query);
$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>';
} ?>
</div>
</div>
</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
$i
counter and conditional break if your sql ensures that not more than 3 rows are returned?