Show Images field in Search Results (Drupal 7)

Show Images field in Search Results (Drupal 7)

marko 25.07.2017
503

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

Top 10 movies from this year, what You must see in 2018!

Why wait until December to name the movies you've absolutely got to see? Our best movies of 2017 list starts…

Making Of "The Bad Batch"

Unbelievably, given the violence that Arlen (Suki Waterhouse) endures in The Bad Batch and its ties to that which…

Show Images field in Search Results (Drupal 7)

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

Armie Hammer, the next James Bond

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

Choose ProcessWire as Your Next CMS!

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

Top 10 movies from a last year, what You must see in 2017!

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

Ryoji Ikeda in Tallinn, Estonia

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

"Lemonade" the best ad from this year

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