我已经将自定义 Google map 脚本实现为 Wordpress 短代码,但我对 .js
文件中定义但由 Wordpress 通过 PHP 生成的某些值有疑问
这是 .js
文件:
//set your google maps parameters
var latitude = 41.03328,
longitude = 21.30281,
map_zoom = 16;
//google map custom marker icon - .png fallback for IE11
var is_internetExplorer11= navigator.userAgent.toLowerCase().indexOf('trident') > -1;
var marker_url = ( is_internetExplorer11 ) ? 'img/cd-icon-location.png' : 'img/cd-icon-location.svg';
//define the basic color of your map, plus a value for saturation and brightness
var main_color = '#00e1ff',
saturation_value= -20,
brightness_value= 5;
我需要做的是在线获取图片的Wordpress主题目录:
'img/cd-icon-location.png' : 'img/cd-icon-location.svg'
对于 var main_color = '#00e1ff'
,从 PHP 获取这个值
<?php echo oneengine_option( 'main_color' ); ?>
最佳答案
我通常使用 PHP 获取 JS 值的做法是在导入 JS 脚本或编写的代码之前声明它们
<?php
echo '<script type="text/javascript">var main_color = "'.$main_color.'";</script>';
?>
<script type="text/javascript" src="someJSScript.js">
//If not in separate file, JS code will go here
</script>
关于javascript - 将 PHP 变量传递给 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27697270/