Tạo danh sách bài viết ngẫu nhiên cho Blogspot

Tạo danh sách bài viết ngẫu nhiên cho Blogspot

Tạo danh sách bài viết ngẫu nhiên cho Blogspot sẽ thay đổi mỗi khi truy cập mới hoặc load lại trang, code rất nhẹ bởi không phải load ảnh thumbnail. Và có thể tùy biến số bài hiển thị cho mỗi lần truy cậy tuy nhiên không theo nhãn.
Loading ...
Chèn trực tiếp đoạn code bến dưới vào phần HTML của trang, bài viết Blgospot hoặc Thêm tiện ích HTML/Javascript bằng cách Vào: Blog => Bố cục => Thêm tiện ích => HTML/Javascript => Dán đoạn code dưới đây vào và lưu lại.
<style type="text/css">
#rtd-random {
    margin: 0 auto;
    width: 100%;
    padding: 20px
}
#rtd-random ul {
    border: 1px solid #e3e3e3;
    list-style: none;
    margin: 0;
    padding: 0
}
#rtd-random li {
    display: block;
    clear: both;
    overflow: hidden;
    list-style: none;
    border-bottom: 1px solid #e3e3e3;
    word-break: break-word;
    padding: 10px 0;
    margin: 0 10px;
}
#rtd-random li:last-child {
    border-bottom: 0;
}
#rtd-random li a {
    color: #444;
    text-decoration: none;
}
#rtd-random li a:hover {
    color: #28a1f0;
}
</style>
<div id='rtd-random'>Loading ...</div>
<script>
//<![CDATA[
// Random Post Widget
var homePage = 'https://gemgai.com',
    maxResults = 10,
    containerId = 'rtd-random';

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function shuffleArray(arr) {
    var i = arr.length,
        j, temp;
    if (i === 0) return false;
    while (--i) {
        j = Math.floor(Math.random() * (i + 1));
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
    return arr;
}

function rtdRandomPosts(json) {
    var startIndex = getRandomInt(1, (json.feed.openSearch$totalResults.$t - maxResults));
    // console.log('Get the post feed start from ' + startIndex + ' until ' + (startIndex + maxResults));
    document.write('<scr' + 'ipt src="' + homePage + '/feeds/posts/summary?alt=json-in-script&orderby=updated&start-index=' + startIndex + '&max-results=' + maxResults + '&callback=randomPosts"></scr' + 'ipt>');
}

function randomPosts(json) {
    var link, ct = document.getElementById(containerId),
        entry = shuffleArray(json.feed.entry),
        skeleton = "<ul>";
    for (var i = 0, len = entry.length; i < len; i++) {
        for (var j = 0, jen = entry[i].link.length; j < jen; j++) {
            link = (entry[i].link[j].rel == "alternate") ? entry[i].link[j].href : '#';
        }
        skeleton += '<li><a href="' + link + '">' + entry[i].title.$t + '</a></li>';
    }
    ct.innerHTML = skeleton + '</ul>';
}
document.write('<scr' + 'ipt src="' + homePage + '/feeds/posts/summary?alt=json-in-script&max-results=0&callback=rtdRandomPosts"></scr' + 'ipt>');
//]]>
</script>
- homePage = 'https://gemgai.com': Thay bằng link Web/ Blog của bạn.
- maxResults = 10 : Số bài viết hiển thị.
Thầy Hiếu Bụng Bự

Chuyến phiêu lưu của đời là học hỏi. Mục đích của đời là trưởng thành. Bản tính của đời là thay đổi. Thách thức của đời là vượt qua. Tinh túy của đời là quan tâm. Cơ hội của đời là phụng sự. Bí mật của đời là dám làm. Hương vị của đời là giúp đỡ. Vẻ đẹp của đời là cho đi.

Post a Comment

Previous Post Next Post