1

您好目前我在我的 wordpress 主题中使用简单天气 JS(http://simpleweatherjs.com/)来显示实时天气以及我使用 redux 框架的选项。我为“您的城市名称”创建了一个文本字段,我的选项唯一 ID 是 >“cpv-hm-cat-op-8”。我的 Redux 框架选项的预览如下。

array(
'id'       => 'cpv-hm-cat-op-8',
'type'     => 'text',
'title'    => __( 'Name of your city', 'redux' ),
'desc'    => __( 'Write your city which you want to show in weather', 'redux' ),
'default'  => 'New York',
), 

现在我想在我的简单天气 JS 文件中使用该选项,并且我想要我的输出,当我输入我的城市名称时,它会在该城市的天气中显示。简单的天气 js 选项看起来像 =>

  $.simpleWeather({
    location: 'New Jersey',
    woeid: '',
    unit: 'c',
    success: function(weather) {
      html = '<h2><i class="symbol-weather  icon-'+weather.code+'"></i> '+weather.temp+'&deg;'+weather.units.temp+'</h2>';
      html += '<ul><li>'+weather.city+'</li>';  
      $("#weather").html(html);
    },
    error: function(error) {
      $("#weather").html('<p>'+error+'</p>');
    }
  });

在这里“位置”我想添加我的 redux 插件选项。但它不工作。

  $.simpleWeather({
    location: '<?php global $cpv; echo $cpv['cpv-hm-cat-op-8'];?>',

请帮助我如何添加我的 redux 框架选项。

4

1 回答 1

2

我找到了一个解决方案,它对我有用,但我不知道它是否是理想的解决方案。

我的解决方案是将值添加到页面头部,以便进一步挂钩。确保在需要此代码的代码之前加载此代码(将所有其他内容加载到它曾经所在的页脚)

在function.php中添加

add_action ( 'wp_head', 'yourjs' );
function yourjs(){ global $cpv; //your global redux Var ?>
  <script type="text/javascript"> 
    var weatherinfo = <?php echo json_encode( array( 
         'city' => $cpv['cpv-hm-cat-op-8'],
         'zip' => $cpv['cpv-hm-cat-op-7'], // just an example for another value in the array
       ) ); ?>
  </script><?php
}

这将放在页面的标题中,例如:

<script type="text/javascript"> 
    var weatherinfo = {"city":"Austin","zip":"12345"}  </script>

所以现在你有一个可以在代码中访问的值数组 - 只需调用 var.

希望这对您有所帮助。

于 2016-09-03T09:06:01.077 回答