4

The default search results in D7 seem to include details of the node author, date, number of comments as seen here but I don't want these in my search results.

I have copied search-result.tpl.php and search-results.tpl.php to my theme file. I have added

print '<pre>'. check_plain(print_r($info_split, 1)) .'</pre';

to search-result.tpl.php. The result is here ie showing the array with [user] [date] [comment] etc

What do I need to do to exclude author / date / number of comments from the search results? If it involves my theme's search-result.tpl.php what should be the contents of search-result.tpl.php ?

1 Answer 1

4
  • Copy search-result.tpl.php into your theme folder..

Override above tpl using below code..

<?php

/**
 * @file
 * Default theme implementation for displaying a single search result.
 *
 * This template renders a single search result and is collected into
 * search-results.tpl.php. This and the parent template are
 * dependent to one another sharing the markup for definition lists.
 *
 * Available variables:
 * - $url: URL of the result.
 * - $title: Title of the result.
 * - $snippet: A small preview of the result. Does not apply to user searches.
 * - $info: String of all the meta information ready for print. Does not apply
 *   to user searches.
 * - $info_split: Contains same data as $info, split into a keyed array.
 * - $module: The machine-readable name of the module (tab) being searched, such
 *   as "node" or "user".
 * - $title_prefix (array): An array containing additional output populated by
 *   modules, intended to be displayed in front of the main title tag that
 *   appears in the template.
 * - $title_suffix (array): An array containing additional output populated by
 *   modules, intended to be displayed after the main title tag that appears in
 *   the template.
 *
 * Default keys within $info_split:
 * - $info_split['module']: The module that implemented the search query.
 * - $info_split['user']: Author of the node linked to users profile. Depends
 *   on permission.
 * - $info_split['date']: Last update of the node. Short formatted.
 * - $info_split['comment']: Number of comments output as "% comments", %
 *   being the count. Depends on comment.module.
 *
 * Other variables:
 * - $classes_array: Array of HTML class attribute values. It is flattened
 *   into a string within the variable $classes.
 * - $title_attributes_array: Array of HTML attributes for the title. It is
 *   flattened into a string within the variable $title_attributes.
 * - $content_attributes_array: Array of HTML attributes for the content. It is
 *   flattened into a string within the variable $content_attributes.
 *
 * Since $info_split is keyed, a direct print of the item is possible.
 * This array does not apply to user searches so it is recommended to check
 * for its existence before printing. The default keys of 'type', 'user' and
 * 'date' always exist for node searches. Modules may provide other data.
 * @code
 *   <?php if (isset($info_split['comment'])): ?>
 *     <span class="info-comment">
 *       <?php print $info_split['comment']; ?>
 *     </span>
 *   <?php endif; ?>
 * @endcode
 *
 * To check for all available data within $info_split, use the code below.
 * @code
 *   <?php print '<pre>'. check_plain(print_r($info_split, 1)) .'</pre>'; ?>
 * @endcode
 *
 * @see template_preprocess()
 * @see template_preprocess_search_result()
 * @see template_process()
 *
 * @ingroup themeable
 */
?>
<li class="<?php print $classes; ?>"<?php print $attributes; ?>>
  <?php print render($title_prefix); ?>
  <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>
</li>
  • Clear Cache

Author / date / number of comments from the search results is getting rendered using below code

<?php if ($info): ?>
  <p class="search-info"><?php print $info; ?></p>
<?php endif; ?>

which we removed from above overridden tpl....

You can also remove it using below function in your theme template.php

function YOURTHEME_preprocess_search_result(&$variables) {
  $variables['info'] = '';
}
4
  • Pay attention that there're 2 templates relevant to the search results page 'search-result.tpl.php' and 'search-results.tpl.php' :) one for the page and one for each result.
    – rreiss
    Apr 25, 2014 at 13:21
  • I have clearly mentioned search-result.tpl.php .. I don't get the point out of above comment... Is it pointed to OP ?
    – Anil Sagar
    Apr 25, 2014 at 14:14
  • Yes, and for other readers as well. It may be handy so I thought to mention it.
    – rreiss
    Apr 25, 2014 at 20:16
  • +1 for helpful answer. Any idea how to override/theme the default page title and move the search box on the results page? I'd rather have the heading "Search Results" be more prominent.
    – eljefejb
    Jul 10, 2014 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.