WordPress 매개 변수를 통해 일부 템플릿을 읽는 방법

5938 단어 WordPresstech
2020년 12월 31일 version 5.5부터 매개 변수 전달 시작
https://make.wordpress.org/core/2020/07/17/passing-arguments-to-template-files-in-wordpress-5-5/

사용법


호출자


get_template_part( "articles", null, array( "title" => "タイトル" ) );

받는 사람


echo esc_html( $args["title"] );
여러 상황에서 array만 추가하면 OK
get_template_part( "articles", null, array( "title" => "タイトル","description" => "ディスクリプション" ) );
그나저나 버젼 5.5 이전 상황은 이렇게 적혀있어요.
functions.php 등
쓰기
function get_template_part_args($template_name, $args = null) {
  if (isset($args)) {
    foreach ($args as $key => $value) {
      set_query_var($key, $value);
    }
  }
  get_template_part($template_name);
}

호출자


get_template_part_args( "articles", array( "title" => "タイトル" ) );

받는 사람


echo esc_html( $args["title"] );
set_query_var의 함수는 다음과 같다.
https://developer.wordpress.org/reference/functions/set_query_var/

좋은 웹페이지 즐겨찾기