简体   繁体   English

在Google Map Infowindow中获取Wordpress发布内容

[英]get wordpress post content in google map infowindow

I have created a googlemaps based branches page, in which the places are created through a custom post type and a google map Advanced Custom Fields object. 我创建了一个基于googlemaps的分支页面,在该页面中,这些位置是通过自定义帖子类型和Google地图高级自定义字段对象创建的。

the only problem is: if I want to have the content of the post inside the infoWindow (to show the location's details etc.) and the content have more then one line - the code breaks... 唯一的问题是:如果我想在infoWindow中放置帖子的内容(以显示位置的详细信息等),而内容又多了一行,则代码会中断...

for example 例如

this will work fine: 这将正常工作:

Lorem ipsum dolor sit amet, id malorum numquam per

but this will break: 但这会中断:

Lorem ipsum dolor sit amet,
id malorum numquam per

this is the code for getting the location content: 这是获取位置内容的代码:

        var locations = [<?php while( $wp_query->have_posts() ){
        $wp_query->the_post();
        $location = get_field('google_map'); 
        ?>
        //acf location's details array
        ['<h1><?php the_title(); ?></h1><h2><?php the_field('branch_name'); ?></h2><div id=<?php echo $post->ID; ?>><?php echo $post->post_content; ?></div>', 
        <?php echo $location['lat']; ?>, <?php echo $location['lng'];?>, <?php $NUM++ ?>],
        <?php } ?> ];

and this is how I get it to the infowindow: 这就是我将其发送到信息窗口的方式:

            google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
                infowindow.setContent(locations[i][0]);
                infowindow.open(map, marker);
                for (var j = 0; j < markers.length; j++) {
                markers[j].setIcon("<?php echo get_template_directory_uri(); ?>/images/mapmarker.png");
            }
            marker.setIcon("<?php echo get_template_directory_uri(); ?>/images/mapmarker-active.png");
        };
    })(marker, i));

here is a codeshare link to the full template's code: http://www.codeshare.io/zaj6n 这是指向完整模板代码的代码共享链接: http : //www.codeshare.io/zaj6n

use json_encode to print strings which contain linebreaks(or characters that would be problematic in JS, eg quotes): 使用json_encode来打印包含换行符(或在JS中会出现问题的字符,例如引号)的字符串:

echo json_encode($post->post_content);

To avoid the double-quotes created by json_encode populate a PHP-array with the desired values and print the entire array by using json_encode : 为避免由json_encode创建的双引号填充一个具有所需值的PHP数组,并使用json_encode打印整个数组:

(not tested) (未测试)

<?php 
  $locations=array();
  while( $wp_query->have_posts() ){
        $wp_query->the_post();
        $location = get_field('google_map');
        $locations[]=array(the_title('<h1>','</h1>',false).
                             '<h2>'.get_field('branch_name').'</h2>'.
                             '<div id="'.$post->ID.'">'.$post->post_content.'</div>',
                           $location['lat'],
                           $location['lng'],
                           $NUM++
                           ); 
  }
  echo "\nvar locations=".json_encode($locations).";\n";

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM