2010-03-01 94 views
1

是否有可能(如果是这样请解释如何)回声的PHP到JavaScript,专门为我的目的我试图呼应自定义字段从wordpress平台的输入到描述的谷歌地图。我希望我可以给客户端一个cms后端来输入地图上标记点上显示的文本。我试图用没有成功是:回声的PHP到谷歌地图javascript

var point = new GLatLng(49.295308,-123.149297); 
var marker = createMarker(point,"Site Title",'<div class="maptext"><p class="prepend-top caption">Title<\/p> 

    <?php $the_query = new WP_Query('category_name=featured'); 
    while ($the_query->have_posts()) : $the_query->the_post();?> 
    <?php if (get_post_meta($post->ID, 'site-description', true)) { ?> 
    <?php echo get_post_meta($post->ID, 'site-description', $single = true); ?> 
    <?php } ?> 
    <\/div>') 
      map.addOverlay(marker); 

确定sarfaz是对他的一部开拓创新的反应,我得到这是打破它解析错误。什么终于摸索是这样的:

var point = new GLatLng(48.134239,-122.764769); 
     var marker = createMarker(point,"Port Townsend Marine Science Center",'<div class="maptext"><?php $the_query = new WP_Query('post_name=test-site'); 
while ($the_query->have_posts()) : $the_query->the_post();?><?php if (get_post_meta($post->ID, 'map-content', true)) { ?><?php echo get_post_meta($post->ID, "map-content", $single = true); ?><?php } ?><?php endwhile; ?><\/div>') 
     map.addOverlay(marker); 

--- UPDATE ---

只是想补充一点,我觉得这是对我抢的帖子,因为我总是想一个特定联系的最佳方式该标记:

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; 
echo $title; 
echo $my_post->post_content; 
?>') 
     map.addOverlay(marker); 
+0

也发布生成的源代码;你的JS可能有一个错误。 – mpen 2010-03-01 06:58:07

+0

在js中没有错误,至少根据萤火虫,并且该地图很好,直到我尝试注入此php – Zac 2010-03-01 07:43:25

回答

1

是的,这确实是一个可以让PHP在javascript代码回荡。在你的代码中你缺少 endwhile,所以只有你的代码的第一行随后执行,导致你意外的结果。

更新:尝试牛逼他:

var point = new GLatLng(49.295308,-123.149297); 
var marker = createMarker(point,"Site Title","<div class=\"maptext\"><p class=\"prepend-top caption\">Title</p> 

    <?php $the_query = new WP_Query('category_name=featured'); 
    while ($the_query->have_posts()) : $the_query->the_post();?> 
    <?php if (get_post_meta($post->ID, 'site-description', true)) { ?> 
    <?php echo get_post_meta($post->ID, 'site-description', $single = true); ?> 

    </div>") 
      map.addOverlay(marker); 
    .................. 
+0

谢谢尝试,但逃避报价并没有帮助。我一直在尝试很多变化,包括删除所有的CSS,看看它是否有帮助,但似乎没有任何工作 – Zac 2010-03-01 07:41:54

+0

啊,你第一次是对的。这只是在php中的一个分析错误。谢谢! – Zac 2010-03-05 03:19:54

+0

@zac:这是好消息:) – Sarfraz 2010-03-05 04:49:29

0

我认为这个问题简单地来自于你的JavaScript字符串产生新的线路。 代码的输出将是这样的:

var marker = createMarker(point,"Site Title","<div class="\maptext\"><p class=\"prepend-top caption\">Title</p> 
    thedatafromyourquery 
    </div>") 

你想拥有的是这样的:

var marker = createMarker(point,"Site Title","<div class="\maptext\"><p class=\"prepend-top caption\">Title</p>" + 
    "thedatafromyourquery" + 
    "</div>") 

希望这有助于。

+0

谢谢,但没有帮助 – Zac 2010-03-02 03:37:39

+0

是否有可能发布整个生成的JavaScript源代码? – kufi 2010-03-02 19:03:54