In this article, we will explore creating a search page in WordPress. A search page allows visitors to search for specific content on your website. We will cover important aspects such as creating a search page template, linking to the search page, customizing the search page, preserving search results, and displaying the total number of results.
Things You Need to Know
Before creating a search page, there are a few things you need to know:
- WordPress has built-in search functionality that allows users to search for content based on keywords.
- WordPress includes a search form that can be added to your website’s sidebar, header, or footer by default.
- To create a dedicated search page, you must create a custom search page template or use a page builder plugin that provides a search element.
Creating a Search Page Template
To create a search page template, follow these steps:
- Access your WordPress theme files through a file manager or FTP client.
- Locate the page.php file in your theme’s folder and create a duplicate copy.
- Rename the identical file as search.php.
- Open the search.php file in a text editor.
- Customize the template to display the search results using the WordPress loop and appropriate HTML markup. You can refer to the WordPress Codex or theme developer documentation for customizing the search template.
- Save the changes and upload the search.php file back to your theme folder.
Creating a Search Page
To create a search page in WordPress, follow these steps:
- Login to your WordPress admin dashboard.
- Navigate to “Pages” and click “Add New” to create a new page.
- Enter a title for your search page, such as “Search” or “Find Content.”
- In the page editor, add the search form using the built-in WordPress search widget or a search block provided by your page builder plugin.
- Save the page.
Linking to Your Search Page
There are several ways to link to your search page:
Using the Page ID
- Go to the “Pages” screen in the WordPress admin dashboard.
- Locate your search page and note down its page ID.
- Use the following URL structure to link to your search page: https://yourdomain.com/?page_id=PAGE_ID, replacing PAGE_ID with the actual page ID of your search page.
Using the Page Slug
- Go to the “Pages” screen in the WordPress admin dashboard.
- Locate your search page and note down its page slug.
- Use the following URL structure to link to your search page: https://yourdomain.com/page-slug, replacing page-slug with the actual slug of your search page.
Using wp_list_pages()
If you have a navigation menu on your website, you can use the wp_list_pages() function to generate a link to your search page. Add the following code to your theme’s template files or navigation menu:
wp_list_pages(‘title_li=&include=PAGE_ID’);
Replace PAGE_ID with the actual page ID of your search page.
Customizing Your Search Page
You can customize your search page by modifying the template (search.php). This allows you to control search results layout, styling, and display. Customize the template according to your theme’s design and requirements.
Preserving Search Page Results and Pagination
WordPress automatically preserves search page results and pagination. When a user performs a search and clicks on a search result, they will be taken to a single post or page. The search query will still be preserved, allowing them to return to the search results if needed.
Display Total Results
To display the total number of search results on your search page, you can use the following code within your search page template (search.php):
<?php
global $wp_query;
$total_results = $wp_query->found_posts;
echo ‘<p>Total Results:.’ $total_results . ‘</p>’;
?>
This code retrieves the total search results from the global $wp_query object and outputs it on the search page.
By following these steps, you can create a search page in WordPress and customize it to suit your website’s design and functionality. A dedicated search page enhances user experience by providing a focused and efficient way to search for content on your website.