^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_search_form()用法示例

get_search_form( bool $echo = true )

Display search form.


描述

Will first attempt to locate the searchform.php file in either the child or the parent, then load it. If it doesn’t exist, then the default search form will be displayed. The default search form is HTML, which will be displayed. There is a filter applied to the search form HTML in order to edit or replace it. The filter is ‘get_search_form’.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

This function is primarily used by themes which want to hardcode the search form into the sidebar and also by the search widget in WordPress.

There is also an action that is called whenever the function is run called, ‘pre_get_search_form’. This can be useful for outputting JavaScript that the search relies on or various formatting that applies to the beginning of the search. To give a few examples of what it can be used for.


参数

$echo

(bool)
(Optional)
Default to echo and not return the form.

Default value: true


返回值

(string|void) String when $echo is false.


源代码

File: wp-includes/general-template.php

function get_search_form( $echo = true ) {
	/**
	 * Fires before the search form is retrieved, at the start of get_search_form().
	 *
	 * @since 2.7.0 as 'get_search_form' action.
	 * @since 3.6.0
	 *
	 * @link https://core.trac.wordpress.org/ticket/19321
	 */
	do_action( 'pre_get_search_form' );

	$format = current_theme_supports( 'html5', 'search-form' ) ? 'html5' : 'xhtml';

	/**
	 * Filters the HTML format of the search form.
	 *
	 * @since 3.6.0
	 *
	 * @param string $format The type of markup to use in the search form.
	 *                       Accepts 'html5', 'xhtml'.
	 */
	$format = apply_filters( 'search_form_format', $format );

	$search_form_template = locate_template( 'searchform.php' );
	if ( '' != $search_form_template ) {
		ob_start();
		require( $search_form_template );
		$form = ob_get_clean();
	} else {
		if ( 'html5' == $format ) {
			$form = '<form role="search" method="get" class="search-form" action="' . esc_url( home_url( '/' ) ) . '">
				<label>
					<span class="screen-reader-text">' . _x( 'Search for:', 'label' ) . '</span>
					<input type="search" class="search-field" placeholder="' . esc_attr_x( 'Search &hellip;', 'placeholder' ) . '" value="' . get_search_query() . '" name="s" />
				</label>
				<input type="submit" class="search-submit" value="'. esc_attr_x( 'Search', 'submit button' ) .'" />
			</form>';
		} else {
			$form = '<form role="search" method="get" id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) ) . '">
				<div>
					<label class="screen-reader-text" for="s">' . _x( 'Search for:', 'label' ) . '</label>
					<input type="text" value="' . get_search_query() . '" name="s" id="s" />
					<input type="submit" id="searchsubmit" value="'. esc_attr_x( 'Search', 'submit button' ) .'" />
				</div>
			</form>';
		}
	}

	/**
	 * Filters the HTML output of the search form.
	 *
	 * @since 2.7.0
	 *
	 * @param string $form The search form HTML output.
	 */
	$result = apply_filters( 'get_search_form', $form );

	if ( null === $result )
		$result = $form;

	if ( $echo )
		echo $result;
	else
		return $result;
}

更新日志

Version 描述
2.7.0 Introduced.

More Information

Usage



get_search_form( $echo );


相关函数

Uses

  • wp-includes/theme.php:
    current_theme_supports()
  • wp-includes/l10n.php:
    _x()
  • wp-includes/l10n.php:
    esc_attr_x()
  • wp-includes/formatting.php:
    esc_url()
  • wp-includes/general-template.php:
    get_search_query()
  • wp-includes/general-template.php:
    search_form_format
  • wp-includes/general-template.php:
    get_search_form
  • wp-includes/general-template.php:
    pre_get_search_form
  • wp-includes/link-template.php:
    home_url()
  • wp-includes/plugin.php:
    do_action()
  • wp-includes/plugin.php:
    apply_filters()
  • wp-includes/template.php:
    locate_template()
  • Show 7 more uses


Used By

  • wp-includes/widgets/class-wp-widget-search.php:
    WP_Widget_Search::widget()

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 5You must log in to vote on the helpfulness of this note

    Contributed by Codex

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

    
    /**
     * Add HTML5 theme support.
     */
    function wpdocs_after_setup_theme() {
    	add_theme_support( 'html5', array( 'search-form' ) );
    }
    add_action( 'after_setup_theme', 'wpdocs_after_setup_theme' );
    

    WordPress will render its built-in HTML5 search form:

    
    <form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
    	<label>
    		<span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ) ?></span>
    		<input type="search" class="search-field"
    			placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ) ?>"
    			value="<?php echo get_search_query() ?>" name="s"
    			title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />
    	</label>
    	<input type="submit" class="search-submit"
    		value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" />
    </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.

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

    
    <form action="/" method="get">
    	<label for="search">Search in <?php echo home_url( '/' ); ?></label>
    	<input type="text" name="s" id="search" value="<?php the_search_query(); ?>" />
    	<input type="image" alt="Search" src="<?php bloginfo( 'template_url' ); ?>/images/search.png" alt="wordpress函数get_search_form()用法示例" title="wordpress函数get_search_form()用法示例" />
    </form>
    

    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:

    
    <input type="hidden" value="post" name="post_type" id="post_type" />
    

    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.

    
    /**
     * Generate custom search form
     *
     * @param string $form Form HTML.
     * @return string Modified form HTML.
    function wpdocs_my_search_form( $form ) {
    	$form = '<form role="search" method="get" id="searchform" class="searchform" action="' . home_url( '/' ) . '" >
    	<div><label class="screen-reader-text" for="s">' . __( 'Search for:' ) . '</label>
    	<input type="text" value="' . get_search_query() . '" name="s" id="s" />
    	<input type="submit" id="searchsubmit" value="'. esc_attr__( 'Search' ) .'" />
    	</div>
    	</form>';
    
    	return $form;
    }
    add_filter( 'get_search_form', 'wpdocs_my_search_form' );
    

    Default HTML4 Form
    If you don’t have searchform.php in your theme, WordPress will render its built-in search form:

    
    <form role="search" method="get" id="searchform"
    	class="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    	<div>
    		<label class="screen-reader-text" for="s"><?php _x( 'Search for:', 'label' ); ?></label>
    		<input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" />
    		<input type="submit" id="searchsubmit"
    			value="<?php echo esc_attr_x( 'Search', 'submit button' ); ?>" />
    	</div>
    </form>
    

    The above form is used on HTML4 websites.

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈