是否有可能(如果是这样请解释如何)将php回复到javascript中,特别是出于我的目的,我试图将自定义字段的输入从wordpress平台回显到谷歌地图的描述中。我希望我可以给客户端一个cms后端来输入地图上标记点中出现的文本。我没有成功的尝试是:
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);
---更新---
只是想补充一点,我发现这是我抓住帖子的最佳方式,因为我总是想要一个特定的链接到该标记:
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 :(得分: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);
..................
答案 1 :(得分: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>")
希望这有帮助。