WordPress:Creating a Search Page

来自站长百科
Xxf3325讨论 | 贡献2008年7月24日 (四) 17:58的版本 (新页面: __TOC__ WordPress automatically generates a web page for search results, but how would you like to have a custom search page for your visitors? Using the WordPress WordPress:Pages fea...)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航、​ 搜索

WordPress automatically generates a web page for search results, but how would you like to have a custom search page for your visitors? Using the WordPress WordPress:Pages feature, you can create a custom search page with information that will help your users search your website.

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 [[WordPress:#Using the page.php|these instructions]]. If it does not, we have the information you need to [[WordPress:# Creating a Search Page Template|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 WordPress: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 include (TEMPLATEPATH . '/searchform.php'); ?>
  • 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
*/
?>

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 [[WordPress:#Using the page.php|above]].

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

thumb|120px|right|Manage Pages PanelNow that you have created your custom Search Page, you can customize the display. From the Manage > Pages panel, on your custom Search Page line, choose Edit. Or open your searchpage.php in 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 include (TEMPLATEPATH . '/searchform.php'); ?>

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