Making a search go directly to an entry if there is only one result

| No Comments | No TrackBacks |
Some of your visitors may know exactly what they are looking for on your blog: that one article you wrote two years ago which had 'bacon sandwich' in the text.  So they type 'bacon sandwich' into your search form, and indeed: one result found!  But now they have to click a link, and that is so much work...

Maybe there is a faster way...
Here is a nifty code snippet you can add to the 'Search Results' template of your Movable Type blog: 

<mt:if tag="searchresultcount" eq="1">
<mt:SearchResults>
<script type="text/javascript">window.location="<mt:entrypermalink>";</script>
</mt:SearchResults>
</mt:if> 

The best place to add this is just before the normal <mt:searchresults> loop. Basically what this does is add a single line of javascript to the output in case there is only one search result. This line of code redirects the browser directly to the article in question. Try it out on this blog: look for 'bacon sandwich' using the search form up there, and you should find yourself back at this article!

No TrackBacks

TrackBack URL: https://www.movabletips.com/cgi-bin/mt/mt-tb.cgi/6

Leave a comment