代码之家  ›  专栏  ›  技术社区  ›  Zac

将php嵌入google地图javascript

  •  1
  • Zac  · 技术社区  · 16 年前

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

    好吧,萨法兹对他的原始反应是正确的,我得到了一个解析错误,它破坏了它。最终奏效的是:

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

    ---更新---

    我只是想补充一点,我发现这是我抓取帖子的最佳方式,因为我总是希望有一个特定的链接到该标记:

      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 回复  |  直到 16 年前
        1
  •  1
  •   Sarfraz    16 年前

    是的,这当然有可能让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);
        ..................
    
        2
  •  0
  •   kufi    16 年前

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

    希望这有帮助。

    推荐文章