Codex tools: Log in
Contents |
A Search Page is a WordPress 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.
To create your own custom Search Page, you will need to create a Page template to include your search form and the information you want your users to see before they search your site.
Check your WordPress Theme to see if it includes a page.php template file. The Default WordPress Theme does include this template, but many do not. If it does, then follow these instructions. If it does not, we have the information you need to create your own.
1. Using a text editor, open the page.php and save as searchpage.php. (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.)
2. After saving it, edit the file:
<?php get_search_form(); ?>
<?php /* Template Name: Search Page */ ?>
You are now ready to customize the look of your Search Page.
If you do not have a page.php, you can create one based upon your Theme's index.php template file.
Search results and Pagination may stop working when applying customization to the search template. To avoid these issues the first thing any developer needs to do is add the following code to the start of their Search template to ensure that the original WordPress query is preserved. To customize the query append additional arguments to (array) $search_query. Execute the $search_query through a new $wp_query object, more information on the WP_Query object can be found at https://codex.wordpress.org/Class_Reference/WP_Query
<?php
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array();
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach
$search = new WP_Query($search_query);
?>
Additional customization arguments can be found on the Codex at https://codex.wordpress.org/Class_Reference/WP_Query
To access the total number of search results from a search, you should retrieve the total number of posts found using the wp_query object.
<?php global $wp_query; $total_results = $wp_query->found_posts; ?>
More information on WP Query can be found on the Codex at https://codex.wordpress.org/Class_Reference/WP_Query
You can now make a link to your custom Search Page in several ways.
<a href="index.php?page_id=17" title="Search Page">Search Page</a>
OR
<a href="<?php bloginfo('url'); ?>/?page_id=17">Search Page</a>
<a href="/wordpress/search-page" title="Search Page">Search Page</a>
OR
<a href="<?php bloginfo('url'); ?>/wordpress/search-page" title="Search Page">Search Page</a>
Above the include function for your searchform.php within the content div, you can add text to help visitors search your site.
<p> My Site features articles about <a title="WordPress Articles" href="index.php?cat=4">WordPress</a>, <a title="Web Design Articles" href="index.php?cat=5">web page design</a>, <a title="Development Articles" href="index.php?cat=6">website development</a>, and <a title="CSS Articles" href="index.php?cat=7">CSS</a>. </p> <p>To search my website, please use the form below.</p> <?php get_search_form(); ?>
You might want to include a list of keywords or other information, images, or details to customize your custom Search Page.