dsyq40772 2010-03-01 06:33
浏览 69
已采纳

Echo php进入谷歌地图javascript

Is it possible (and if so please explain how) to echo php into javascript, specifically for my purpose I am trying to echo the input of a custom field from the wordpress platform into the description of a google map. I am hoping I can give the client a cms backend to input the text that appears in the marker points on a map. What I am trying with no success is:

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);

ok sarfaz was right with his orignal response and I was getting a parse error which was breaking it. what finally worked was this:

 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 ---

Just wanted to add that I found this to be the best way for me to grab posts since I always want a specific one linked to that marker :

  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);
  • 写回答

2条回答 默认 最新

  • douliang2935 2010-03-01 06:37
    关注

    Yes, that is surely possible to have php echoed in javascript code. In your code you are missing the endwhile so only first subsequent line of your code is executing causing you unexpected result.

    Update: Try t his:

    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);
        ..................
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭