Skip to main content

Creating a Search Page


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.

Using the page.php

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:
  • 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 style sheet, 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 Panels:
<?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).
  • In the Administration Panels go to Write > Write Page.
    WordPress Write Page Panel
  • In the title field enter Search.
  • Do not write anything in the content area.
  • From the Page Template drop-down menu select: Search Page
  • Click the Create New Page button
  • Select the Manage Pages panel.
  • Write down the page_id number of your newly created Page named Search Page.
You are now ready to customize the look of your Search Page.

Creating a Search Page Template

If you do not have a page.php, you can create one based upon your Theme's index.php template file.
  1. In a text editor, open your theme's index.php and save as searchpage.php.
  2. Then follow the editing steps from above.

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 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 athttps://codex.wordpress.org/Class_Reference/WP_Query

Display Total Results

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

Linking to Your Search Page

You can now make a link to your custom Search Page in several ways.
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.
<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>
Using the Page Slug 
The Page slug is set in the Edit Page panel. 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:
<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>
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

Manage Pages Panel
Now that you have created your custom Search Page, you can customize the display. From theManage > Pages panel, on your custom Search Page line, choose Edit. Or open your searchpage.phpin a text editor and edit it there.
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.

Comments

Popular posts from this blog

உடல் எடையை குறைக்க வேண்டுமா ?

இன்றைய அவசர உலகின் மிக பெரிய பிரச்சனையாக இருப்பது உடல் எடை அதிகரிப்பது தான்.மனம் போன போக்கில் உணவு கட்டு பாடு இல்லாமல் கண்டதையும் உள்ளே தள்ளுவதும்,உக்காந்த இடத்திலேயே கணணி முன் நேரத்தை விரயமாக்குவதும் தான் இந்த பிரச்சனைக்கு மூல காரணமாகும். அது சரி இந்த பிரச்சனையை எப்படி இல்லாமல் செய்வது அல்லது உடல் எடையை எவ்வாறு குறைப்பது என்பதை பற்றி பாப்போம் , பல வருட ஆரய்சிக்குபின் மருத்துவர்கள்   உடல் எடைய குறைக்க மிகவும் சுலபமான உடற்பயிற்சியை கண்டுபிடித்துள்ளனர்.இது  100% பயனளிக்க கூடியது, எந்த இடத்திலும் எந்தநேரத்திலும் மிக சுலபமா செய்ய கூடிய உடற் பயிற்சியாகும்.இந்த உடற்பயிற்சிகள் படத்துடன் கீழே தரப்பட்டுள்ளது நீங்களும் முயற்சித்து பாருங்கள கண்டிப்பாக பலன் கிடைக்கும்... முதலில் நாற்காலியில் உட்கார்ந்து இட  பக்கம் பார்கவும் .. ..        அடுத்து  நாற்காலியில் உட்கார்ந்து வல  பக்கம் பார்கவும்  ....  நண்பர்கள் யாரவது மச்சி வாடா சின்ன பீஸ் ,இங்க பாரு சூப்பர் அய்டம்னு சொல்லி கால்ல விழுந்து கூப்பிட்டலோ மேற்கூறிய உடற் பயிற்சிகளை முயற்சித்து பார்கவும் கண்டிப்பாக பலன் கிடைக்கும் .

38 (new) web tools to keep you busy

For many of us, the internet represents our daily job, income resource or biggest hobby. Every day we check our emails, read our feeds, visit our websites, find and discuss new things and GOD knows what else. It requires a lot of tools to do all this stuff and sometimes, we forget to search for easier solutions losing valuable time or keeping down the production graph. It's very hard to keep track with everything that's new and popular and this is why we do monthly searches for the best web tools out there. Enjoy!  45+ Web Operating Systems "There are many more web operating systems hoping to bring all your usual desktop applications online in one place - here are more than 45 of our favorites."  15 Ways To Create Website Screenshots "15 Ways To Create Website Screenshots"  Open Source Windows "The promise of open source software is best quality, flexibility and reliability. The only way to have TRUE "Open Source Windows"is

40 Fresh & Beautiful Examples of Websites With Large Backgrounds

Using large sized pictures or illustrations as your website’s background adds a great visual appeal to your website’s design. Many web designers use large images as backgrounds as more and more users are now opting for high resolution monitors and high speed internet connections. Here’s a showcase of 40 Fresh and amazing websites that are using large background images. 1.  The Pixel Blog 2.  Copimaj Interactive 3.  Flourish Web Design 4.  Abduction Lamp 5.  Morphix Design Studio 6.  Final Phase 7.  Make Photoshop Faster 8.  WebSarga 9.  Suie Paparude 10.  Duirwaigh Studios 11.  BlackMoon Design 12.  Sepitra 13.  Le Blog de Gruny 14.  Piipe 15.  Mozi Design Studio 16.  Electric Current 17.  Lora Bay Golf 18.  Life Style Sports 19.  ligne triez 20.  Oliver Kavanagh 21.  World of Merix Studio 22.  Le Web Defi 23.  How host 24.  Productive Dreams 25.  Gary Birnie 26.  Glocal Ventures 27.  GDR UK 28.  Absolute Bica 29.  Le Nordik 30.  Leaf Tea Shop & Bar 31.  Paul Smith 32.  EwingCole