2

I want to print custom text if there is no result on search.

I'm using custom search-result.tpl.php and search-results.tpl.php.

My search-results.tpl.php is that:

     <?php if ($search_results): ?>
      <h2><?php print t($search_totals);?></h2>
      <div class="view-content">
        <table class="views-table cols-4 search-results <?php print $module; ?>-results table table-striped">
.../* my results style here */
        <tbody>
            <?php print $search_results; ?>
        </tbody>
        </table>
      </div>
      <?php print $pager; ?>


    <?php else : ?>
      <h2><?php print t('Your search yielded no results');?></h2>
      <?php print search_help('search#noresults', drupal_help_arg()); ?>
    <?php endif; ?>


It works correctly if there are search results but if there is no result it doesn't print anything although there is the <?php else : ?> part.


How can I print custom text if there is no result?

1 Answer 1

2

The core Drupal search module uses several template files that dictate both how search results are displayed, and what's displayed when there are no search results.

If you're already using a custom theme on your Drupal site, the best way to customize those would be to copy and paste the entire 'search-results.tpl.php' file from the search/ module folder into your own custom theme subfolder.

Open that file up for editing and you'll see the following at the bottom:

<?php else : ?>
  <h2><?php print t('Your search yielded no results');?></h2>
  <?php print search_help('search#noresults', drupal_help_arg()); ?>
<?php endif; ?>

This is what's displayed if search results are not found (thus the 'else')... Modify the text to be anything you want here. The second line, which calls the search_help() function is the one that displays the search suggestions. Feel free to replace that too....

And as always, before you can see your changes, you'll probably need to clear cache!

1
  • I have already had this two files (search-result.tpl.php and search-results.tpl.php) inside of my theme folder. And as you can see in my question there are the lines you mentioned about. There is the <?php else : ?> piece but it doesn't work (doesn't print anything).
    – herci
    Commented Nov 25, 2014 at 11:40

Your Answer

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

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