2017-11-19 4 views
0

는 내가 를 사용하려고 워드 프레스 테마, 에서 자바 스크립트 파일에서 이미지를 표시하는 문제에 직면하고있어하지만, JS 파일에 작동하지 않습니다Probelm는

document.getElementById("women-eyes1").src="<?php echo 
    esc_url(get_template_directory_uri());?> images/ss1.png"; 

는 어떻게 JS 파일에 이미지에 안내 할 수 있습니다 : 그것은 JS 파일에 beacuse

때문에이 라인은 작동하지 않는 이유는 무엇입니까?

답변

0

js 파일 file.js에서 PHP 함수를 사용할 수 없습니다.

:

<?php 

// Register the script 
wp_register_script('some_handle', 'path/to/myscript.js'); 

// Localize the script with new data 
$translation_array = array(
    'some_string' => __('Some string to translate', 'plugin-domain'), 
    'a_value' => '10' 
); 
wp_localize_script('some_handle', 'object_name', $translation_array); 

// Enqueued script with localized data. 
wp_enqueue_script('some_handle'); 

는 다음의 js 파일에 변수를 사용

// some php file 
// output the script 
?> 
<script> 
    document.getElementById("women-eyes1").src="<?php echo 
    esc_url(get_template_directory_uri());?> images/ss1.png"; 
</script> 
<?php // rest of your php 

또는이 같은 값을 wp_localize_script을 사용하고 전달할 수 있습니다 :이처럼 PHP 파일에서 인라인 스크립트를 사용할 수 있습니다

<script> 
// alerts 'Some string to translate' 
alert(object_name.some_string); 
</script> 
+0

wp_localize_script로 사진을 추가하려면 어떻게해야합니까? –