简体   繁体   English

如何使用简单天气 JS 的 Redux 框架文本字段选项

[英]How to use Redux Framework Text Field option for Simple Weather JS

Hello Currently i am using Simple weather JS ( http://simpleweatherjs.com/ ) in my wordpress theme for showing live weather and also for options i am using redux framework.您好目前我在我的 wordpress 主题中使用简单天气 JS ( http://simpleweatherjs.com/ ) 来显示实时天气以及我正在使用 Z7490FE17CAEC373F2299D65B6E20E88 框架的选项i've create a text field for "Name of your City" and my Option Unique ID is > " cpv-hm-cat-op-8 ".我为“您的城市名称”创建了一个文本字段,我的选项唯一 ID 是 >“cpv-hm-cat-op-8”。 Preview of My Redux framework option is below.我的 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',
), 

Now i want to use that option in my simple weather JS file and i want my output, when i input name of my city its show in weather of that city.现在我想在我的简单天气 JS 文件中使用该选项,我想要我的 output,当我输入我的城市名称时,它会在该城市的天气中显示。 simple weather js option look like =>简单的天气 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>');
    }
  });

Here "Location" i want to add my redux plugin option.在这里“位置”我想添加我的 redux 插件选项。 but its not working.但它不工作。

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

Kindly Help me how can i add there my redux framework option.请帮助我如何添加我的 redux 框架选项。

I've found a solution, it works for me, but I don't know if it's the ideal solution.我找到了一个解决方案,它对我有用,但我不知道它是否是理想的解决方案。

My solution is adding the value to the head of the page, so it's accessable for further hooks.我的解决方案是将值添加到页面头部,以便进一步挂钩。 Make sure this code will be loaded before your code which needs this code (fe load all other stuff to the footer where it's used to be)确保在需要此代码的代码之前加载此代码(将所有其他内容加载到它曾经所在的页脚)

in function.php add在 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
}

this will be put in the header on the page like:这将放在页面上的 header 中,例如:

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

so now you have an array of values you can access in your code - just call the var.所以现在你有一个可以在代码中访问的值数组 - 只需调用 var.

Hope this helps you.希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM