回声样式到谷歌地图和wordpress自定义字段的混搭

时间:2010-03-07 05:11:14

标签: php javascript wordpress google-maps

使用wordpress,我会从特定帖子中提取自定义字段,以填写Google生成的地图的内容。我正在使用此代码

  var point = new GLatLng(48.5139,-123.150531);
  var marker = createMarker(point,"Lime Kiln State Park", 
    '<?php $post_id = 182;
$my_post = get_post($post_id);
$title = $my_post->post_title;
$snip = get_post_meta($post_id, 'mapExcerpt', true);
echo $title;
echo $snip;
?>')
      map.addOverlay(marker);

我正在尝试回显css样式块,但这会导致javascript错误

  var point = new GLatLng(48.5139,-123.150531);
  var marker = createMarker(point,"Lime Kiln State Park", 
    '<?php $post_id = 182;
$my_post = get_post($post_id);
$title = $my_post->post_title;
$snip = get_post_meta($post_id, 'mapExcerpt', true);
echo "<div class='theTitle'>";
echo $title;
echo "</div>";
echo $snip;
?>')
      map.addOverlay(marker);

我收到错误

missing ) after argument list

,输出

     var point = new GLatLng(48.5139,-123.150531);
  var marker = createMarker(point,"Lime Kiln State Park",
   '<div class='theTitle'>Site Title</div>Site excerpt')
   map.addOverlay(marker); 

有人可以告诉我一个更优雅(工作)的解决方案吗?

1 个答案:

答案 0 :(得分:0)

您生成的代码在传递给 createMarker 的单引号中没有未转义的单引号。

您需要更改:

echo "<div class='theTitle'>";

为:

echo "<div class=\"theTitle\">";

所以输出将是:

var point = new GLatLng(48.5139,-123.150531);
var marker = createMarker(point,"Lime Kiln State Park",
   '<div class="theTitle">Site Title</div>Site excerpt')
map.addOverlay(marker);