简体   繁体   English

无论如何使用api将显示数量限制为2条评论

[英]is there anyway to limit display number to 2 reviews on google places using api

I'm using webflow to display google reviews on my website using Google API, for now, everything ok but instead display 5 reviews I would like to display only 2我正在使用 webflow 使用 Google API 在我的网站上显示 google 评论,目前,一切正常,但显示 5 条评论,我只想显示 2 条

I already a code to display the reviews but is there anyway to add in my code to display only 2 reviews?我已经有一个代码来显示评论,但无论如何要添加我的代码以仅显示 2 条评论?

<script>
    var map = document.getElementById('google-places');
    function initMap() {
        var service = new google.maps.places.PlacesService(map);
        service.getDetails({
            placeId: 'xxxxxxxxxxxxxxxxx'
        }, function (places, status) {
            if (status === google.maps.places.PlacesServiceStatus.OK) {
                reviewsArray = [];
                reviewsArray.push(places.reviews);
                for (var key in reviewsArray) {
                    var arr = reviewsArray[key];
                    for (i = 0; i < arr.length; i++) {
                        var review = arr[i];
                        var author = review.author_name;
                        var when = review.relative_time_description;
                        var comment = review.text;
                        var starNumber = review.rating;
                        var starPercentage = `${(starNumber / 5) * 100}%`;
                        console.log(starPercentage);
                        var profilePic = review.profile_photo_url;
                      //  console.log(author + ', ' + when + ', ' + comment + ', ' + rating + ', ' + profilePic);

document.getElementById('google-places').innerHTML += ` ${author} ☆☆☆☆☆ ★★★★★ document.getElementById('google-places').innerHTML += ` ${author} ☆☆☆☆☆ ★★★★★

        </div>

    </div>
    <div class="review-content-wrapper">
        <div class="review-text">${comment}</div>
    </div>


    </div>
    </div>

                                `
                        }
                    }
                }
            })
        }
    </script>

<!-- Replace XXX with your key that you created at https://console.cloud.google.com -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=xxxxxxxxxxxxxxxxx&libraries=places&callback=initMap">
</script>

It appears Google Reviews defaults to 5 reviews (source: Is it possible to get latest five google reviews from google places api? ).似乎 Google 评论默认为 5 条评论(来源: 是否可以从 google place api 获取最新的 5 条 Google 评论? )。

Could you use only the first two and ignore the last three?你可以只使用前两个而忽略后三个吗?

You should be able to do this in your own code;你应该能够在你自己的代码中做到这一点; it isn't API dependent;它不依赖于 API; if you use Jquery or PHP for example you can loop through the array and only display the reviews with index 0 and 1 for example to only show the two and leave out the rest.例如,如果您使用 Jquery 或 PHP,您可以遍历数组并仅显示索引为 0 和 1 的评论,例如仅显示两者而忽略其余部分。

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

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