【问题标题】:Meta values in wordpress linked to Google Maps lat,lng APIwordpress 中的元值链接到 Google Maps lat,lng API
【发布时间】:2015-12-08 22:35:35
【问题描述】:

我有一个自定义函数,可以在 Wordpress 中添加一个纬度和一个经度元框。

我在我的functions.php中使用这样的Google Maps API

function googlemaps() {
    echo "<script>

    var map;
    function initMap() {
        center: {lat: -34.397, lng: 150.644},
        zoom: 8
      });
    }

        </script>
     ";
    }

它适用于静态 lat 和 lng 值,但我无法让它适用于我的元值

我这样做了,这显然是错误的,但据我所知

function googlemaps() {
    echo "<script>

    var map;
    function initMap() {
      map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: get_post_meta( get_the_ID(), '_lat', lng: get_post_meta( get_the_ID(), '_boyta'},
        zoom: 8
      });
    }

        </script>
    ";
}

【问题讨论】:

    标签: php wordpress google-maps


    【解决方案1】:

    你需要跳出你的“echo”语句来打印get_post_meta的结果。现在,您只是在打印文本。试试这个:

    function googlemaps() {
      echo "<script>
      var map;
      function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: " . get_post_meta( get_the_ID(), '_lat', true ) . ", lng: " . get_post_meta( get_the_ID(), '_boyta', true ) . "},
          zoom: 8
        });
      }
    
    </script>";
    }
    

    我假设您的 lat 和 long 存储为“_lat”和“_boyta”?我添加的 "true" 告诉 WordPress 您需要单个值,而不是值数组。

    【讨论】:

    • 完美运行!谢谢你:)