WordPress:Creating a Search Page

来自站长百科
跳转至: 导航、​ 搜索

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.

WordPress为搜索自动产生网页,但是你想要为访客提供一个自定义的搜索网页吗?使用WordPress网页功能,你可以创建自定义的搜索网页,网页上提供信息,帮助你的用户搜索你的网站。

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.

不同的WordPress主题拥有不同的模板文件。有的主题包含search.php模板文件。这不是搜索网页,这只是显示搜索结果的模板。也有个称为searchform.php的模板文件。这个模板文件通常包含在许多主题的边栏中而且产生搜索框的形式。如果你的主题中没有这个模板文件,你可以轻易地从默认主题中复制这个模板文件。

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

查看你的WordPress主题,看看这个主题是否包含page.php模板文件。默认WordPress主题的确包含这个模板,但是大多数主题不包含这个模板。如果主题的确包含这个模板,请遵循[[WordPress:#Using the page.php|这些指示说明]]。如果你的主题不包含这个模板,我们有信息,能够使你[[WordPress:# Creating a Search Page Template|创建你自己的]]模板。

Using the page.php[ ]

使用 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.)

1.使用文本编辑器,打开page.php,并且保存为searchpage.php。(注:文件名search.php保存为特殊的模板名,因此避免其用法;暗示的searchpage.php使得在一列文件中识别,变得简单。)

2. After saving it, edit the file: 2.保存后,编辑文件:

  • Delete WordPress:The Loop (i.e. basically everything within your content div), leaving the div tags intact.
  • 删除The Loop(例如,一般来说,你的内容div内的任何内容),保留完整的div标签。
  • 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.
  • 添加标头例如"搜索文章"或者其它相似的内容。你可以从CSS样式表中使用当前的class,或者创建新的class。
  • Copy the following into the content div or any other div that contains the content of your Page:
  • 将下面复制到内容div或者其它包含你的网页内容的div
<?php include (TEMPLATEPATH . '/searchform.php'); ?>
<?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:
  • 在你的searchpage.php的顶上方,在所有内容的前面,添加这个,赋予搜索网页标头,WordPress在管理面板中能够识别这个标头:
<?php
/*
Template Name: Search Page
*/
?>


<?php
/*
模板名: 搜索网页
*/
?>


  • Save the file
  • 保存文件
  • Upload the file to your theme directory (if you made changes to your style.css style sheet file, upload that, too).
  • 将文件上传到你的主题目录(如果你更改了你的tt>style.css样式表文件,上传这个文件)。
  • In the title field enter Search.
  • 在标题区,输入搜索
  • Do not write anything in the content area.
  • 不要在内容区,编写任何内容。
  • Click the Create New Page button
  • 点击创建新网页按钮
  • Write down the page_id number of your newly created Page named Search Page.
  • 写下你最新创建的命名为搜索网页网页 page_id数目。

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.

如果你没有page.php,你可以根据你的主题的index.php模板文件,创建一个。

  1. In a text editor, open your theme's index.php and save as searchpage.php.
  1. 文本编辑器中,打开你的主题的index.php并且保存为searchpage.php
  1. Then follow the editing steps from [[WordPress:#Using the page.php|above]].
  1. 然后遵循下面的来自[[WordPress:#Using the page.php|上面的]]编辑步骤。

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>

;使用网页ID:不管你是否使用permalinks,你可以通过使用网页的网页ID数字,链接到你的新的搜索网页。<pre><a href="index.php?page_id=17" title="Search Page">搜索网页</a>

OR
或者
<a href="<?php bloginfo('url'); ?>/?page_id=17">Search Page</a>

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

;使用网页Slug:网页Slug设置在编辑网页面板。如果你使用Permalinks,这是网页的名称。你可以动手更改。网页slug链接的一个例子是:

<a href="/wordpress/search-page" title="Search Page">Search Page</a>

<pre><a href="/wordpress/search-page" title="Search Page">搜索网页</a>

OR
或者
<a href="<?php bloginfo('url'); ?>/wordpress/search-page" title="Search Page">Search Page</a>

<a href="<?php bloginfo('url'); ?>/wordpress/search-page" title="搜索网页">搜索网页</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.

;使用wp_list_pages() :如果你正在使用wp_list_pages()模板标签,网页名会自动地在你的网页列表上产生。

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.

thumb|120px|right|管理网页面板既然你已经创建了自定义的搜索网页,你可以自定义显示。从管理 > 网页面板,在你定义搜索网页命令上,选择编辑。或者在文本编辑器中打开你的searchpage.php,并且对其进行编辑。

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

上述你的内容div中关于searchform.phpinclude函数,你可以添加文本帮助访客搜索你的站点:

<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'); ?>






<p>
我的站点上的文章包括
<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>要搜索我的网站,请使用下面的形式。</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.

你可能想要包含关键词或者其它信息,图像或者详细信息的列表,自定义你的自定义搜索网页。