search en Drupal 7: How to add a custom CSS class to the last search result item on the search page <span>Drupal 7: How to add a custom CSS class to the last search result item on the search page</span> <span><span>admin</span></span> <span>Wed, 11/14/2012 - 13:53</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>This is a small tidbit of information in the event that you wanted to alter the Drupal search results page. You can add a custom CSS class to the last search result item (for whatever reason you may have). In my case, I wanted to remove the border-bottom from the last result, so I had to add a special CSS class to do this. Just follow these simple steps:</p> <ol><li>Override <a href="!search!" rel="nofollow">template_preprocess_search_results</a> Here is how to alter the code. This goes in your template.php: <code> function yourthemename_preprocess_search_results(&amp;$variables) { $variables['search_results'] = ''; if (!empty($variables['module'])) { $variables['module'] = check_plain($variables['module']); } //checking the total number of results $num_results = count($variables['results']); $counter = 0; foreach ($variables['results'] as $result) { $counter++; if ($num_results == $counter) { //means we have the last result so we add the class $variables['search_results'] .= theme('search_result', array('result' =&gt; $result, 'module' =&gt; $variables['module'], 'last' =&gt; 'last')); } else { $variables['search_results'] .= theme('search_result', array('result' =&gt; $result, 'module' =&gt; $variables['module'])); } } $variables['pager'] = theme('pager', array('tags' =&gt; NULL)); $variables['theme_hook_suggestions'][] = 'search_results__' . $variables['module']; } </code></li> <li>Now we override <a href="!search!search-result.tpl.php/7" rel="nofollow">search-result.tpl.php</a> Create this file and put it in your custom theme folder.</li> <li><code>"&lt;?php print $attributes; ?&gt;&gt; &lt;?php print render($title_prefix); ?&gt; </code> <p><code>&gt; <a rel="nofollow">"&gt;&lt;?php print $title; ?&gt;</a> </code></p> <p> <code> &lt;?php print render($title_suffix); ?&gt; </code><code>&lt;?php if ($snippet): ?&gt; </code></p> <p><code>&gt;&lt;?php print $snippet; ?&gt;</code></p> <p> <code> &lt;?php endif; ?&gt; &lt;?php if ($info): ?&gt; </code></p> <p><code>&lt;?php print $info; ?&gt;</code></p> <p> <code> &lt;?php endif; ?&gt; </code><code> </code></p></li> <li>Clear your cache</li> </ol><p>Now if you search for something, you will notice that your very last search result has the CSS class of "last". This also works for search results that have a pager. That is, the last result on every page will have the class of "last".</p> </div> <div class="field field--name-taxonomy-vocabulary-2 field--type-entity-reference field--label-above"> <div class="field__label">Tags</div> <div class="field__items"> <div class="field__item"><a href="/tag/drupal" hreflang="en">Drupal</a></div> <div class="field__item"><a href="/tag/drupal-7" hreflang="en">Drupal 7</a></div> <div class="field__item"><a href="/tag/search" hreflang="en">search</a></div> <div class="field__item"><a href="/tag/tutorial" hreflang="en">tutorial</a></div> <div class="field__item"><a href="/tag/drupal-planet" hreflang="en">Drupal Planet</a></div> </div> </div> <section class="comments" > <h2>Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=50&amp;2=comment_node_page&amp;3=comment_node_page" token="ApDbkPDuzm4sAIjju5T7a0FpK23QAQ3xpyKXVp7CfGo"></drupal-render-placeholder> </section> <div id="field-language-display"><div class="js-form-item form-item js-form-type-item form-item- js-form-item-"> <label>Language</label> Not specified </div> </div> Wed, 14 Nov 2012 17:53:23 +0000 admin 50 at