user2897448
user2897448

Reputation:

How to display articles' image in Joomla search results

Is there a way to modify the code of the Joomla's search component so it can display even the article's image?

I have made a simple code that can search the image or youtube video in the article's text and return the media including the required tags as a string, but where do I put it?

// SEARCH IMAGE OR VIDEO
$hasImage = 0;
$hasVideo = 0;
$articleMedia = '';
$articleText = $text;

// has image?
preg_match_all('/<\s?img[^>]+\>/i', $articleText, $matches);
    if(isset($matches['0']['0']))
        {
        $articleMedia = $matches['0']['0'] ;
        $hasImage = 1;
        }

// no image? maybe video
if ($hasImage == 0) {
    preg_match_all('/(http:\/\/www\.youtube\.com\/watch\?v=([a-zA-Z0-9_-]+))/i', $articleText, $matches);
    if(isset($matches['0']['0']))
        {
        $articleMediaID = $matches['2']['0'] ;
        $articleMedia = "//www.youtube.com/embed/" . $articleMediaID;
        $hasVideo = 1;
        } else {
            preg_match_all('/(http:\/\/youtu.be\/([a-zA-Z0-9_-]+))/i', $articleText, $matches1);
            if( isset($matches1['0']['0']) ){
                $articleMediaID = $matches1['2']['0'] ;
                $articleMedia = "//www.youtube.com/embed/" . $articleMediaID;
                $hasVideo = 1;
                }
        }


    if ( $hasVideo == 1){
            $articleMedia = '<iframe class="youtube" width="100%"  src="' . $articleMedia . '" frameborder="0" allowfullscreen></iframe>';
            }
}

// END SEARCH IMAGE OR VIDEO

Upvotes: 0

Views: 1168

Answers (1)

Craig
Craig

Reputation: 9330

You can create a search plugin, it's very simple to to create one, especially as you've worked out your core code already.

Depending on the type of Joomla search you're using you should read either:

  1. Creating a Search Plugin
  2. Creating a Smart Search Plugin

Upvotes: 0

Related Questions