A WordPress search Page is just a page with a custom Page template to give users more information for searching your site. Different WordPress Themes feature different template files. Some include a search.php template file. This is not a Search Page, it is merely a template that displays the search results. There is also a template file called searchform.php. This is a template file that is often included in 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.
This article will cover two things. One is on how you can create a WordPress search page and the other is on how to create a search results page.
Edit the search.php and searchform.php method
- Using a text editor, open the page.php and save as searchpage.php. If you do not have a page.php, you can create one based upon your Theme’s index.php template file.
Note: The filename search.php is reserved as a special template name, so avoid its usage; the suggested searchpage.php just makes it easy to recognize in the list of files.
- After saving it, edit the file:
- Delete The Loop (i.e. basically everything within your content div), leaving the div tags intact.
- Add a heading such as “Search Posts” or something similar. You can use an existing class from your CSS stylesheet, or create a new one.
- Copy the following into the content div or any other div that contains the content of your Page:
<?php get_search_form(); ?>
- At the top of your searchpage.php, before anything else, add this to give your Search Page a heading WordPress will recognize in the Administration Screens:
<?php /** * Template Name: Search Page */ ?>
- Save the file
- Upload the file to your theme directory (if you made changes to your style.css style sheet file, upload that, too).If you create searchpage.php from page.php in Twenty Seventeen, it would be as like as followings:
<?php /** * Template Name: Search Page */ ?> <?php get_header(); ?> <div class="wrap"> <div id="primary" class="content-area"> <main id="main" class="site-main" role="main"> <?php get_search_form(); ?> </main><!-- #main --> </div><!-- #primary --> </div><!-- .wrap --> <?php get_footer();
Based on the Search Page Template, we will create the search page.
- In the Administration Screen go to Pages > Add New.
- In the title field enter Search.
Do not write anything in the content area.
- While still on the same page, look for Page Attributes on the right side menu.
- Select the drop-down menu in Template, and select Search Page.
- Click the Publish button.
There are several options you can use to customize your internal search results pages. While you could always dive into the search.php code (though this is best left to experienced users comfortable with PHP and CSS coding), there are several plugins you can use to implement similar changes. Finding the functionality that works best will depend on you and your site. Small personal sites may benefit from free plugins, while larger sites might need a premium solution.
To do this manually you will need to start by creating a child theme.
Next, create a search.php file in your brand new child theme and copy over the code from your old theme (you can find this by either opening up theme files on your server via FTP, or from your WordPress dashboard under Appearance > Editor > Search.php). Now you can replace the default title in your child theme’s search.php with the following:
<h1 class="search-title"> <?php echo $wp_query->found_posts; ?> <?php _e( 'Search Results Found For', 'locale' ); ?>: "<?php the_search_query(); ?>" </h1>
This will display the title with the count of the posts found followed by the term that was searched. So it would look something like “15 Search Results Found For: My Search Query”.
If you are looking for a simpler method to add a WordPress search page, then follow the steps below.
Add WordPress search page using ExpertRec site search
Here we will visit the steps required to add the ExpertRec Custom search engine to your website. This is one of the easiest setup processes of all the available options out there and is highly recommended.
- Navigate to https://cse.expertrec.com/newuser?platform=cse and signup with your Google ID.
- Enter your website’s URL when prompted. You can select a server location near you and add the URL of your sitemap if you wish to. These will be auto-detected otherwise.
- You can play around with the settings and customize the UI as the crawl runs. Once it is complete, you can check out a fully functional demo before taking the search to your website.
- You can take the search to your website with little to no effort. All you need to do is to paste the code snippet available on the dashboard on your website.
ExpertRec comes with more customization options that you can explore. You can read this article to find a more detailed guide on the installation and configuration.
By adding this to your website you not only get a fully customizable WordPress search page, but also a search result page out of the box without any additional configuration required. If you wish to make any changes, it can be done via the ExpertRec control panel without any coding.