要将Google评论小部件添加到网站,您可以按照以下步骤进行操作:
<div id="google-reviews"></div>
<script>
function loadGoogleReviews() {
var script = document.createElement('script');
script.src = 'https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initGoogleReviews';
document.body.appendChild(script);
}
function initGoogleReviews() {
var reviews = new google.maps.places.ReviewsService();
reviews.getReviews({
placeId: 'YOUR_PLACE_ID',
maxResults: 5 // 设置要显示的评论数量
}, function(results, status) {
if (status === 'OK') {
var container = document.getElementById('google-reviews');
for (var i = 0; i < results.length; i++) {
var review = results[i];
var rating = review.rating;
var text = review.text;
var author = review.author_name;
var reviewElement = document.createElement('div');
reviewElement.innerHTML = '<p>评分:' + rating + '</p><p>评论:' + text + '</p><p>作者:' + author + '</p>';
container.appendChild(reviewElement);
}
}
});
}
loadGoogleReviews();
</script>
请确保将YOUR_API_KEY
替换为您在步骤6中复制的API密钥,并将YOUR_PLACE_ID
替换为您希望显示评论的地点的Google Place ID。
领取专属 10元无门槛券
手把手带您无忧上云