Tuesday 1, Apr 2025

Latest News

AdminChèn bài viết ngẫu nhiên vào Blogger

Random Posts cho Blogger
Tôi có tìm kiếm trên mạng hướng dẫn cách hiển thị các bài ngẫu nhiên trong Blogger, nhưng chỉ tìm thấy 2 hướng dẫn của Phydeaux3Purple Moggy. Cả 2 hướng dẫn này chỉ hiển thị 1 bài ngẫu nhiên, không hỗ trợ hiển thị 1 danh sách gồm nhiều bài, vì thế tôi viết bài này để chia sẻ với các bạn có cùng quan tâm. Hi vọng nó có ích với những ai đang dùng Blogger.
Làm theo dạng widgetCách làm này sẽ giúp bạn chèn thêm 1 widget (page element) vào 1 vị trí tùy ý do bạn lựa chọn trong Layout. Để thực hiện, bạn hãy làm theo các bước sau:
- Login vào Dashboard, chọn Layout
- Nhấn vào Add a gatget và chọn kiểu HTML/Javascript
- Phần tiêu đề bạn đặt tùy ý, còn phần nội dung, bạn copy và paste đoạn code sau vào:
<div id="random-posts"></div><script type='text/javascript'> function getRandomPosts(json) {     var maxEntries = 10;     var numPosts = json.feed.openSearch$totalResults.$t;     var indexPosts = new Array();     for (var i = 0; i < numPosts; ++i) {         indexPosts[i] = i;     }     indexPosts.sort(function() {return 0.5 - Math.random()});     if (maxEntries > numPosts) {         maxEntries = numPosts;     }     var container = document.getElementById('random-posts');     var ul = document.createElement('ul');     for (i = 0; i < maxEntries; ++i) {         var entry = json.feed.entry[indexPosts[i]];         var li = document.createElement('li');         var a = document.createElement('a');         a.title = entry.title.$t;         for (var j = 0; j < entry.link.length; ++j) {             if (entry.link[j].rel == 'alternate') {                 a.href = entry.link[j].href;                 break;             }         }         a.appendChild(document.createTextNode(entry.title.$t));         li.appendChild(a);         ul.appendChild(li);     }     container.appendChild(ul); } </script> <script src='/feeds/posts/summary?alt=json-in-script&callback=getRandomPosts&max-results=999999' type='text/javascript'></script><br /> 
 - Để tùy biến số lượng bài viết hiển thị (mặc định là 10), bạn hãy sửa lại giá trị của maxEntries trong dòng thứ 4, thay số 10 bằng số bài sẽ hiển thị mà bạn muốn:
var maxEntries = 10;
- Save lại và thử nghiệm
Chèn vào vị trí bất kì trong blog
Có đôi lúc bạn không muốn chèn vào các widget mà chèn thẳng vào 1 vị trí nào đó trong blog như phía dưới mỗi bài viết chẳng hạn, thì khi ấy bạn hãy làm như sau:
- Login vào Dashboard, chọn Layout và chọn tiếp Edit HTML
- Nhớ nhấn vào Download Full Template để sao lưu template trước khi thực hiện
- Đánh dấu vào ô Expand Widget Templates để hiện thị đầy đủ template
- Tim đến vị trí bạn muốn hiển thị, VD nếu ở dưới mỗi bài viết thì thường vị trí đó là post-footer-line-1
- Chèn đoạn code ở trên vào vị trí đó
Lưu ý: nếu bạn copy trực tiếp đoạn code trên thì có khả năng bị báo lỗi. Nguyên nhân là do các kí tự <, >, & ... đã không được mã hóa đúng. Để khắc phục, bạn hãy vào trang Code Converter để chuyển các kí tự đó sang dạng tương ứng. Sau đó tiến hành chèn lại vào vị trí đã định là được.






  Nếu đoạn code trên không thực thi được thì các bạn thử với đoạn code dưới đây xem sao. Ở đây tôi chỉ thay thế việc thao tác với các DOM node bằng hàm document.write:

<script type='text/javascript'> function getRandomPosts(json) {     var maxEntries = 10;     var numPosts = json.feed.openSearch$totalResults.$t;     var indexPosts = new Array();     for (var i = 0; i < numPosts; ++i) {         indexPosts[i] = i;     }     indexPosts.sort(function() {return 0.5 - Math.random()});     if (maxEntries > numPosts) {         maxEntries = numPosts;     }     document.write('<ul id="random-posts2">');     for (i = 0; i < maxEntries; ++i) {         var entry = json.feed.entry[indexPosts[i]];         title = entry.title.$t;         for (var j = 0; j < entry.link.length; ++j) {             if (entry.link[j].rel == 'alternate') {                 var link = entry.link[j].href;                 break;             }         }         document.write('<li><a href="' + link + '" title="' + title + '">' + title + '</a>');     }     document.write('</ul>'); } </script><br /> <script src='/feeds/posts/summary?alt=json-in-script&callback=getRandomPosts&max-results=999999' type='text/javascript'></script><br />
no image
  • Blogger Comments
  • Facebook Comments

3 Trả lời:

  1. Chen bai ngau nhien vao blog la bai post xuat hien ngau nhien hay la tuong dong voi bai viet dang doc? Neu vay thi thuong nam duoi hoac nam tren bai viet chu nhi?

    Trả lờiXóa
  2. When it comes to the original reason for the split, "empty heart" some regret:. "At uggs on sale the christian louboutin time, my mother was dying in hospital christian louboutin remise 50% and at home mom does cheap christian louboutin not agree with Christian Louboutin Daffodile us, his girlfriend, and Cheap Louis Vuitton Handbags I ugg soldes did not dare offend ends, the ugg boots mood has been bad . Just open the annual ugg australia meeting of the company, I drink a little wine that night, and then Cheap LV Handbags to sing any connection with my colleagues, continue to drink that night, a lot ugg of phone call from his girlfriend, Discount Louis Vuitton she was ready to go uggs outlet home, I want Discount LV Handbags to send Christian Louboutin Bois Dore her, I do not know how you, was upset and say that the one roll. As a result, she really christian louboutin shoes ran away. "After eller this, his girlfriend Bags Louis Vuitton and "empty heart" does not explicitly say that breaking ugg pas cher up, but did not meet again. "Empty Heart" somewhat dissatisfied, feel even break up, and also the discount christian louboutin face clear.

    Trả lờiXóa

Item Reviewed: Chèn bài viết ngẫu nhiên vào Blogger Rating: 5 Reviewed By: Admin
Top