2010-03-01 91 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