Show Images field in Search Results (Drupal 7)

Show Images field in Search Results (Drupal 7)

marko 25.07.2017

How to display Images field in Search Results on Drupal 7. That's a tricky question because 7 is a really old version in php and drupal core dev's was already dreaming about version 8 and totally forgot to make this work :)

So first, open your active theme template.php file and add a code

function MYTHEME_preprocess_search_result(&$variables) {    
    $node = $variables['result']['node'];
    if (is_object($node) && $node->nid) {
        $variables['teaser'] = node_view($node, 'teaser');

Then add your templates folder new file called: search-result--node.tpl.php

<div class="<?php print $classes; ?>"<?php print $attributes; ?>>
  <?php print render($title_prefix); ?>
  <?php print render($teaser['field_images']); ?>
  <h3 class="title"<?php print $title_attributes; ?>>
    <a href="<?php print $url; ?>"><?php print $title; ?></a>
  <?php print render($title_suffix); ?>
  <div class="search-snippet-info">
    <?php if ($snippet): ?>
      <p class="search-snippet"<?php print $content_attributes; ?>><?php print $snippet; ?></p>
    <?php endif; ?>

Now clean your cache and you got your nice search result template.

NB: If you like to display only first image

<?php print render($teaser['field_images'][0]); ?>