2

I want to change the page template when displaying search results. This seems straigh forward when dealing with a content type, but what is the content type for "search results".

E.g. I'll like to use something like page--search_results.tpl.php but I'm not sure of the correct name.

Note, I'm happy with the formatted output of the result items, so I assume I wouldn't be changing the templates under drupal/modules/search...

Thanks

5

You can use a template, eg, page--search.tpl.php which would match the path to search and anything under it, eg, search/node/term, or you could create one called page--search-node.tpl.php which wouldn't match just the search page itself, but only search results from nodes, or you could use a template suggestion inside a preprocess_page() hook, eg, something like

mymoduleortheme_preprocess_page(&vars) {

  if (arg(0)=='search' && arg(1) && arg(2)) { // we are somewhere down in search/node/term or similar
    $vars['theme_hook_suggestions'][] = 'page_my_template_for_search_results';
  }
}

and then create a template file page-my-template-for-search-results.tpl.php.

In any case, http://drupal.org/node/1089656 has some great documentation on template suggestions in D7.

  • Were you using Drupal 6 or 7 when you saw this? Not sure where "args" comes from. – user5681 Mar 18 '12 at 9:06
  • args? I don't follow... – Jimajamma Mar 19 '12 at 4:46
  • Sorry. I'm new to php and wasn't sure what "arg(2)" was refering to as I can't see the variable declared in the method. – user5681 Mar 21 '12 at 6:08
  • 2
    arg() is a drupal function that returns that part of the path, eg, arg(0) returns the 0th (or 1st) part, arg(1) returns the 1st (or 2nd lol) etc. so, if your path is www.myhost.com/search, arg(0) will be search, and arg(1) will be null, as will be arg(2) since they don't exist. but, if you are looking at www.myhost.com/search/node/term, then arg(0) will return search, arg(1) will return node and arg(2) will return term (eg, what you searched for). so if arg(0) equals 'search' and arg(1) and arg(2) are set, you are down in the path of search results. – Jimajamma Mar 21 '12 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy