Show Images field in Search Results (Drupal 7)

Show Images field in Search Results (Drupal 7)

marko 25.07.2017
393

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>
  </h3>
  <?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; ?>
  </div>
</div>

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]); ?>

How to display Images field in Search Results on Drupal 7. That's a tricky question because 7 is a…

Armie Hammer set to be the next James Bond? Rumors that the producers are looking for someone slightly different…

ProcessWire http://processwire.com is an up-and-coming, free, open source PHP CMS and Content Management Framework (CMF). At its heart it’s…

Believe it or not, Shaun The Sheep Movie was recently announced as the highest rated film of the summer…

Ryoji Ikeda is a Japanese sound artist. Ikeda's music is concerned primarily with sound in a variety of "raw"…

Confusion erupts in a neighborhood when two kids set up a lemonade stand with music and TV icon, Ice…

Font Awesome makes it easy to add vector icons and social logos to your website. And Pro gives 1,000+ more…

Making first video for #ProcessWire with json + ionic angular js "Front end, how make mobile app with Processwire"…