I'm trying to display images in search results for the default search of Joomla. I messed with some code from the web and I managed to display images with the following:
in template/html/com_search/com_search/search/default_results.php:
in line 9, I added:
$db = JFactory::getDbo();
$article = JTable::getInstance('Content', 'JTable');
and then above:
<dd class="result-text"> <?php echo $result->text;?></dd>
I added:
<?php
// Joomla content
if( strstr($result->href, 'com_content') )
{
preg_match_all('#\d+#', $result->href, $val);
$id = (int)$val[0][0];
$article->load($id);
}
?>
<?php
$dom = new DOMDOcument;
$dom->loadHTML($article->introtext);
$images = $dom->getElementsByTagName('img');
if (isset($images[0])) {
?>
<img src="<?php echo $images[0]->getAttribute('src'); ?>" class="search-results-img">
<?php
}
?>
The result is images in search results with image class for styling in css, BUT it works only for article search. If search includes categories too, I get:
Warning: DOMDocument::loadHTML(): Empty string supplied as input.
I think the warning is because of $dom->loadHTML($article->introtext);
I want someone to check the code as I think using preg_match_all()
isn't the best practice and I need to include categories image.
$result->href
value so that I can tailor my answer to your question and data? Also, what kind of incoming data do you receive when processing a category? – mickmackusa♦ Dec 13 '19 at 3:25