>  기사  >  백엔드 개발  >  WordPress_php 팁에서 검색 양식을 얻기 위한 PHP 함수 사용 구문 분석

WordPress_php 팁에서 검색 양식을 얻기 위한 PHP 함수 사용 구문 분석

WBOY
WBOY원래의
2016-05-16 20:00:50919검색

get_search_form 함수는 워드프레스에서 미리 설정된 검색 양식이나 기본 검색 양식을 추출하는 데 사용됩니다. 이 공식 기능은 중국어로 제공되지 않기 때문에 간단히 작성했습니다.

설명
get_search_form 함수는 WordPress에서 사용자 정의 검색 양식 또는 기본 검색 양식을 추출하는 데 사용됩니다.
사용자 정의 양식을 표시하거나 기본 양식을 완전히 표시하는 것은 테마에 search.php 파일이 있는지 여부에 따라 다릅니다.
파일이 있으면 해당 파일이 자동으로 호출됩니다. 없으면 기본 검색 양식이 표시됩니다.


사용

<&#63;php
  get_search_form($echo = true) 
&#63;>

매개변수
$echo 부울 유형, 변수를 표시할지 아니면 반환할지 선택하는 데 사용됩니다.
기본값: 참


생각보다 복잡하지 않고 실제로는 간단합니다.

<&#63;php
  get_search_form(); 
&#63;>

Google 맞춤 검색을 통합해야 하는 경우 여기에 언급하세요.
search.php 파일에 코드의 사용자 정의된 부분을 넣으면 됩니다. 물론 스타일을 설정해야 합니다.

함수 소스 코드

<&#63;php
 /**
 * 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'.
 *
 * 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,
 * '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.
 *
 * @since 2.7.0
 * @param boolean $echo Default to echo and not return the form.
 */
function get_search_form($echo = true) {
 do_action( 'get_search_form' );
 
 $search_form_template = locate_template('searchform.php');
 if ( '' != $search_form_template ) {
 require($search_form_template);
 return;
 }
 
 $form = '<form role="search" method="get" id="searchform" action="' . esc_url( 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>';
 
 if ( $echo )
 echo apply_filters('get_search_form', $form);
 else
 return apply_filters('get_search_form', $form);
}
&#63;>

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.