get_template_directory_uri와 get_stylesheet_directory_uri의 차이

Last Updated: 2015년 08월 18일 | | 댓글 남기기

워드프레스에서 jQuery나 css를 후크(enqueue)할 때 스타일시트의 주소 지정에 get_template_directory_uri나 get_stylesheet_directory_uri를 사용합니다.

예:

wp_enqueue_script('custom-script', get_stylesheet_directory_uri() . '/js/custom_script.js', array( 'jquery' ) ); // 테마 폴더 아래 /js/custom_script.js 파일을 enqueue시킴

그럼 get_template_directory_uriget_stylesheet_directory_uri의 차이는 무엇일까요?

  • get_template_directory_uri는 현재 테마의 디렉터리 URI를 가리킵니다. 자식 테마(차일드 테마)가 사용되는 경우에도 부모 테마 디렉터리 URI가 반환됩니다.
  • get_stylesheet_directory_uri는 현재 테마/자식 테마의 디렉터리 URI를 가리킵니다.  자식 테마(차일드 테마)가 사용되는 경우 자식 테마 디렉터리 URI가 반환됩니다.

둘 사이의 차이점은 get_template_directory_uri는 무조건 부모 테마 디렉터리 URI를 반환하지만, get_stylesheet_directory_uri는 자식 테마가 있을 경우 자식 테마의 디렉터리 URI를 반환한다는 점입니다. 저는 enqueue할 때 보통 get_stylesheet_directory_uri를 사용하고 있으며 실제로도 문제 없이 잘 작동하고 있습니다. 만약 자식 테마가 있는 경우 부모 테마 아래의 폴더를 호출하려면 get_template_directory_uri를 반드시 사용해야 하고, 자식 테마를 사용하지 않을 경우에는 어느 것을 사용하든 무방하겠습니다.

<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/aternus.png" alt="" width="" height="" />

위와 같은 코드의 경우 현재 테마(자식 테마가 있을 경우 자식 테마) 내의 images 폴더 내의 png 파일을 호출하고 있습니다. 자식 테마가 있을 경우 자식 테마 폴더 아래에 /images/ 폴더를 만들어야 합니다.


댓글 남기기

Leave a Comment