首页>文档>WordPress模板标签>get_search_form() – 加载搜索表单

get_search_form() – 加载搜索表单

模板标签

get_search_form( array $args = array() )

加载搜索表单。

说明

首先将会在子主题或者父主题中寻找searchform.php文件,然后加载它。如果这个文件不存在,则会显示默认搜索表单。默认搜索表单是HTML形式的。

此功能主要用于将搜索表单内嵌到主题中或者侧边栏内的小部件里,以便使用。

每当运行该函数时,还会调用一个名为“pre_get_search_form”的操作。

参数

$args

array (可选) 显示参数的数组。

    • ‘echo’
      • (bool)是回显还是返回表格。默认为true。
    • “aria_label
      • (字符串)搜索表单的ARIA标签。有助于区分同一页面上的多个搜索表单并提高可访问性。

默认值:array()

返回

(void | string)如果’echo’参数为true,则无效;如果’echo’为false,则搜索HTML表单。

来源

文件:wp-includes / general-template.php

function get_search_form( $args = array() ) {
    /**
     * 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
     * @since 5.5.0 The `$args` parameter was added.
     *
     * @link https://core.trac.wordpress.org/ticket/19321
     *
     * @param array $args The array of arguments for building the search form.
     */
    do_action( 'pre_get_search_form', $args );
 
    $echo = true;
 
    if ( ! is_array( $args ) ) {
        /*
         * Back compat: to ensure previous uses of get_search_form() continue to
         * function as expected, we handle a value for the boolean $echo param removed
         * in 5.2.0. Then we deal with the $args array and cast its defaults.
         */
        $echo = (bool) $args;
 
        // Set an empty array and allow default arguments to take over.
        $args = array();
    }
 
    // Defaults are to echo and to output no custom label on the form.
    $defaults = array(
        'echo'       => $echo,
        'aria_label' => '',
    );
 
    $args = wp_parse_args( $args, $defaults );
 
    /**
     * Filters the array of arguments used when generating the search form.
     *
     * @since 5.2.0
     *
     * @param array $args The array of arguments for building the search form.
     */
    $args = apply_filters( 'search_form_args', $args );
 
    $format = current_theme_supports( 'html5', 'search-form' ) ? 'html5' : 'xhtml';
 
    /**
     * Filters the HTML format of the search form.
     *
     * @since 3.6.0
     * @since 5.5.0 The `$args` parameter was added.
     *
     * @param string $format The type of markup to use in the search form.
     *                       Accepts 'html5', 'xhtml'.
     * @param array  $args   The array of arguments for building the search form.
     */
    $format = apply_filters( 'search_form_format', $format, $args );
 
    $search_form_template = locate_template( 'searchform.php' );
 
    if ( '' !== $search_form_template ) {
        ob_start();
        require $search_form_template;
        $form = ob_get_clean();
    } else {
        // Build a string containing an aria-label to use for the search form.
        if ( isset( $args['aria_label'] ) && $args['aria_label'] ) {
            $aria_label = 'aria-label="' . esc_attr( $args['aria_label'] ) . '" ';
        } else {
            /*
             * If there's no custom aria-label, we can set a default here. At the
             * moment it's empty as there's uncertainty about what the default should be.
             */
            $aria_label = '';
        }
 
        if ( 'html5' === $format ) {
            $form = '<form role="search" ' . $aria_label . '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" ' . $aria_label . '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
     * @since 5.5.0 The `$args` parameter was added.
     *
     * @param string $form The search form HTML output.
     * @param array  $args The array of arguments for building the search form.
     */
    $result = apply_filters( 'get_search_form', $form, $args );
 
    if ( null === $result ) {
        $result = $form;
    }
 
    if ( $args['echo'] ) {
        echo $result;
    } else {
        return $result;
    }
}

 

用法

get_search_form( $echo );

官方原文档

https://developer.wordpress.org/reference/functions/get_search_form/

个人中心
今日签到
有新私信 私信列表
搜索