get_search_form() 描述


<?php get_search_form$echo ); ?>

get_search_form() 用法



(boolean) (可选) true to echo the form; false to return the form as a string.

默认值: true

get_search_form() 参数



The form's HTML, if the $echo parameter is set to false.

get_search_form() 返回值


Default HTML4 Form

If you don't have searchform.php in your Theme, WordPress will render its built-in search form:

The above form is used on HTML4 websites.

Default HTML5 Form

Since WordPress 3.6, If your theme supports HTML5 Markup, which happens if it uses:

WordPress will render its built-in HTML5 search form:

Among the changes is that the form has a class="search-form". Also, the input is type="search" and not type="text". Furthermore there is a placeholder, which displays text when appropriate, which means you don't need javascript to display the placeholder.
There are no elements with an id anymore, so you can have multiple searchforms in a valid document.

Theme Form

If you do have searchform.php in your Theme, it will be used instead. Keep in mind that the search form should do a GET to the homepage of your blog. The input text field should be named s and you should always include a label like in the examples above.

Example of a custom searchform.php:

The only parameter here that will be submitted is s with the value of the current search query. However, you can refine it in many ways. For example by only showing posts in the search results. This can be done with the next addition to the form above:

Here we submit the value post. The default value is any, meaning, posts, pages and custom post types. With the input above added to the form it will now only search in posts with the post_type post.
There are many additions like this. With a var_dump of the object $wp_query you can see all the default values of the search variables. With a var_dump of $wp_query->query you can see the current query.

A last option is to write a custom function (in your functions.php file) and hook that function to the get_search_form action hook.

get_search_form() 示例


Please use value="<?php echo get_search_query(); ?>" (so a wrapper function for: esc_attr( $s ); and other necessary 过滤器s) if you'd like to display currently searched term in the search field once searching have already occurred. This is one of the most XSS vulnerable places in themes if not secured.

get_search_form() 注意


  • 添加于 版本: 2.7.0

get_search_form() 历史


get_search_form() 函数的代码位于 wp-includes/general-template.php.

get_search_form() 源文件