使用这个教程,我试图将数据从 Wordpress 自定义帖子类型元框中提取到 XML 文件中,然后使用数据库值中的标记设置创建一个谷歌地图。
我添加了一个文件夹并将名为 map/generateMapXml.php 的主题目录归档
生成MapXml.php
<?php
// Create a xml doc for map
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
header("Content-type: text/xml");
//Query the artist for info & coordinates.
$args = array(
'post_type' => 'artists',
'posts_per_page' => -1
);
$query = new WP_Query($args);
if($query->have_posts()): ?>
<?php while($query->have_posts()): ?>
<?php $query->the_post() ?>
<?php
$mapName = get_the_title();
$mapAddress = rwmb_meta('artists_location');
$mapLat = rwmb_meta('artists_lat');
$mapLong = rwmb_meta('artists_long');
$mapType = rwmb_meta('artists_type');
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name",$mapName);
$newnode->setAttribute("address",$mapAddress);
$newnode->setAttribute("lat",$mapLat);
$newnode->setAttribute("lng",$mapLong);
$newnode->setAttribute("type",$mapType);
?>
<?php endwhile ?>
<?php endif ?>
<?php echo $dom->saveXML(); ?>
到目前为止,一切都很好。这将输出正确的 xml,但是当我尝试按照教程说明将文件内容拉入 jquery 时:
downloadUrl("<?php echo get_template_directory_uri(); ?>/map/generateMapXml.php",
function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<b>" + name + "</b> <br/>" + address;
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
});
bindInfoWindow(marker, map, infoWindow, html);
}
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
我收到 500 内部服务器错误:
GET http://localhost/test/number/wp-content/themes/testtheme/map/generateMapXml.php 500 (Internal Server Error) localhost/test/number/:93
downloadUrl localhost/test/number/:93
load localhost/test/number/:49
onload
就好像文件不存在一样。我将文件夹和文件的 chmod 设置为 777。这是由于 Wordpress 导致的错误,还是我的代码中的语法错误?或者很有可能,是否有更简单的方法来创建地图?