A Better Textpattern Search
Published 29 July 04 by Justin French, 27 comments
By default, TXP search results are displayed in the “default” page template, inside the <txp:article /> tag. Whilst I can live with the results being inside this tag, I wanted a separate page template for the search results that looked completely different from my default (home) page.
I also wanted some more control over the XHTML that TXP output for the headings, result count, etc.
Step 1
Before you do anything else, make a back-up. Back-up both your TXP database, and the TXP source code (the /textpattern/ directory). We’re not doing anything to crazy here, but it’s nice to be safe.
Step 2
In Presentation > Pages, create a new “page” template called “search”. I’ll leave the design/structure up to you, but it’s essential that you have a <txp:article /> tag in the <body> of the page.
Step 3
In Presentation > Sections, create a new section called “search”. Set it to use the page template “search” that you just created. Stick with the default style for the moment, and set all the other options to “no”.
Step 4
TXP’s <txp:search_input /> tag will send the search query to the default section, rather than the search section. We could hack the <txp:search_input /> tag to suit our needs or even create a plug-in, but it’s just as easy to do it with a form.
In Presentation > Forms, create a new “misc” form called “search_input” with the following code:
<form action="/index.php" method="get"> <input type="hidden" name="s" value="search" /> <input type="text" name="q" value="" size="15" /> <input type="submit" value="search" /> </form>
The key here is that we’ve added the hidden field “s” to specify that search queries should be pointed to the “search” section.
Step 5
To include the new search form on any of your pages, replace <txp:search_input ... /> with <txp:output_form form="search-input" />.
Step 6
You can (to a certain extent) customise the way TXP displays the search results inside the <txp:article /> tag. Create a new misc. form called “search_results” with the following code:
<div class='search-result'>
<h2><txp:search_result_title /></h2 >
<p class='posted'>Published
<txp:search_result_date /></p>
<txp:search_result_excerpt />
</div>
For each search result, TXP will use this form template to format & display the results. There’s also a <txp:search_result_url /> tag available, but I didn’t need it.
Test it out
Now is a perfect chance to test your new search form, search page, and search results form. Tweak your page templates and forms to suit your own needs, and you’re pretty much done!
From here on, we’re hacking the TXP source to have even more control over the results output.