Theme views search results just like core


By Aaron - Posted on 26 July 2010

The views module is often used in place of or in addition to Drupal's core search module or other search modules when more control is required. By adding exposed filters, a site's visitors can narrow a search by the value of any field and many node object values.

The results can quickly be themed to highlight the search keys just like core search.

Check the theme information for your view for the row style output template to create. But, as the theme info explains, you don't create a views template, you create a node template. If your view is named site_search, then your node template would be node-views-site_search.tpl.php.

I found it best to control the display of links and comments from the views UI in the row style settings, and then render them in the template if you want to highlight and found search keys there.

Add this bit of code at the top of your template and you're good to go. I use $_GET['keys'] because the filter identifier is "keys" for the exposed Search: search terms filter.

<?php

 
// Highlight found search terms.
 
$keys = $_GET['keys'];
 
$content = search_excerpt($keys, $node->body . comment_render($node));

?>

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor.
Fill in the blank