Apache Solr is a very good web search engine and it works very well with Drupal. This post shows you how to customize the default pure text search result template by adding node image to each result record.
First, we need to load the corresponding node for each search result. This can be done in the <theme>_preprocess_search_result(&$variables) function. Add it to the template.php.
template.php
... function <theme>_preprocess_search_result(&$vars) { // Add node object to result, so we can display imagefield images in results. $n = node_load($vars['result']['node']->nid); $n && ($vars['node'] = $n); } ...
Then we can customize the search-result.tpl.php. If you don’t have this file in theme folder, create a new one and copy the source @ Drupal API – search-result.tpl.php source
In the search-result.tpl.php, add the following code to get the node image for each record.
... <p class="search-result-image"><?php print theme('imagecache', '<imagecache_preset>', $node->field_cck_image[0]['filepath']); ?></p> ...
Done =)
Reference: Midwestern Mac, LLC – Adding Images to Search Results (Drupal Search)
I get the following error when I use this method:
Notice: Undefined property: stdClass::$nid in lendit_preprocess_search_result()
LikeLike
are u using Drupal 6 or 7?
and can you paste the code your your function here? as you can see from this article, i didn’t define any $nid.
LikeLike