Wordpress Search Page Template

admin 11/22/2021
74 Comments
  • Since the page template slug is stored inside the postmeta for any post that has been assigned to a page template, it is possible to directly query the postmeta to see whether any given page has been assigned a page template. This is the method that ispagetemplate uses internally.
  • Basically, you need to include the Wordpress loop in your search.php template to loop through the search results and show them as part of the template. Below is a very basic example from The WordPress Theme Search Template and Page Template over at ThemeShaper.

Open the template file that you found in the previous step and find the main loop. The main loop (if there is more than one loop) is the one that has one of the Multiple Posts.

Topics

  • Linking to Your Search Page

Languages: English • 日本語

A Search Page is a WordPress Page with a custom Page template to give users more information for searching your site.

Things You Need to Know Things You Need to Know

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.

Creating a Search Page Template Creating a Search Page Template

1. 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.

2. 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:
  • 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:

3. Save the file.

4. 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:

Creating a Search Page Creating a Search Page

Wordpress search template

Wordpress Page Template Php

Based on the Search Page Template, we will create the search page.

  1. In the Administration Screen go to Pages > Add New.
  2. In the title field enter Search.
    Do not write anything in the content area.
  3. While still on the same page, look for Page Attributes on right side menu.
  4. Select the drop-down menu in Template, and select Search Page.
  5. Click the Publish button.

It will show simple search form such as

Linking to Your Search Page Linking to Your Search Page

You can now make a link to your custom Search Page in several ways.

Wordpress Search Template

Using the Page ID Using the Page ID

Whether or not you use permalinks, you can link to your new Search Page by using Page ID number of the Page. Insert the next line into your any posts, pages or templates

OR you may insert the next line into templates

Using the Page Slug Using the Page Slug

The Page slug is set in the Edit Page screen. It is the name of the page if you are using Permalinks. You can manually change this. An example of a Page slug link would be:

Wordpress Search Page Template Download

for any posts, pages or templates when slug is ‘search-page’. OR you may insert the next line into templates

Using wp_list_pages() Using wp_list_pages()

If you are using the wp_list_pages() template tag, the page name would be automatically generated in your Pages list.

Customizing Your Search Page Customizing Your Search Page

Wordpress Edit Search Page Template

Now that you have created your custom Search Page, you can customize the display. Open your searchpage.php in a text editor and edit it there.

Above the get_search_form() function for your searchform.php within the content div, you can add text to help visitors search your site.

You might want to include a list of keywords or other information, images, or details to customize your custom Search Page.

Preserving Search Page Results and Pagination Preserving Search Page Results and Pagination

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 WP_Query.

Additional customization arguments can be found at WP_Query.

Display Total Results Display Total Results

To access the total number of search results from search.php, a search result page, you should retrieve the total number of posts found using the wp_query object.

More information on WP_Query can be found at WP_Query.