This tutorial is going to show you how to add a search form & edit search results page on your WordPress template.
How to add a search form
Log into your WordPress admin panel and go to the left-side menu Appearance -> Widgets.
- Under the Available Widgets locate a one called Search (A search form for your site) and drag it to one of the boxes available on the right side (depends on the particular template design)
- When needed, specify a title for your Search form and click Save.
- Preview the page which has the widget position with the Search form enabled to see it there.
How to edit the search results page
- In your WordPress admin panel go to Appearance -> Editor.
- From the right-hand side of the Edit Themes page look for the one called Search Results (serach.php) and click on it. The search.php is a template file that represents a template that displays the search results.
- Search Form (searchform.php) you can also select from the right side represents another template file that is often included to the sidebar of many themes and generates the search box form. If there isn’t one in your theme, you can easily copy it from the Default theme.
- Prior to making any changes to the code of these pages, we suggest that you copy it and save in a file on your computer to have for a backup copy.
- The way you need to customize the search results page is totally up to you. For instance, your default search results pages look like this:
- You can look for the text you see on this page (like: Search for, Read More, Older Entries, Newer Entries etc) in the code by hitting Ctrl + F.
- To disable text, for example, the one that says No comments, first look for the text with Ctrl + F to find the line it is in, which is
<?php comments_popup_link('No comments', '1 comment', '% comments', 'comments-link', 'Comments are closed'); ?>
And delete it (comment it out).
- To change the number of search results, put
<?php query_posts('showposts=#'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
where # is the number of posts.
To remove the footer sidebar, remove <?php get_footer(); ?> at the bottom.
- To display the number of results, change
<h1>Search for: "<?php the_search_query(); ?>"</h1>
<h1>Search for: "<?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e(''); _e('<span class="search-terms">'); echo $key; _e('</span>'); _e(' — '); echo $count . ' '; _e('articles'); wp_reset_query(); ?></h1>
- Once you are done with your changes, click on Update File at the bottom to save them and try use the Search box on your site:
Feel free to check the detailed video tutorial below: