Pages

Monday, December 12, 2011

3

Shuffle and limit popular post gadget post count

First look at these two previous articles about customizing popular posts gadget:

Shuffle Blogger popular posts and
Excluding specific popular posts in Blogger.

If you exclude posts from popular posts gadget, then you don't have exact count of popular posts – the count depends on how many excluded posts were in the list.

With this hack you can shuffle and then limit the count of popular posts. If you already have installed the shuffle hack, replace it with this code. You can put this code in a HTML/Javascript gadget, or in html template before </body>.

Data provided by Pastebin.com - Download Raw
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <script type='text/javascript'>
  2. //<![CDATA[
  3. // Shuffle and limit count of Blogger popular posts using jQuery / MS-potilas 2011
  4. // Put this code in a html/javascript gadget or before </body>.
  5. // See http://yabtb.blogspot.com/2011/12/shuffle-and-limit-popular-post-gadget.html
  6. var popcount = 8;
  7. var ul = $('.popular-posts ul');
  8. var lis = $('.popular-posts ul li').detach();
  9. if(lis.length>1) {
  10.   for(var i=lis.length-1;i;i--) {       // fisher-yates/knuth
  11.     var j=parseInt(Math.random()*i);
  12.     lis[j] = lis.splice(i,1,lis[j])[0]; // swap lis[i]&lis[j]
  13.   }
  14.   lis.slice(-popcount).appendTo(ul);
  15. }
  16. //]]>
  17. </script>

This hack is installed on this blog, I use 10 popular posts, from which I exclude the current post user is viewing, and some others. Then the list is shuffled and limited to 8 entries. Modify the popcount variable in the script to get a different amount of posts. In my MS-potilas blog I use five popular posts, picked from ten posts minus some possible excluded posts.
[Hide comments] - [Show comments]
Click on a single comment to hide/show its text

3 comments:

Unknown said April 18, 2012 at 2:35 PM

I am currently excluding some posts, but when I do that, the popular post gadget also exclude the number of posts. You said that this html will fix that, so that '8' is for the number of posts I want? or is part of the code?
BTW, Thanks Very Much for the help you have provided me, I appreciate that a lot. :)

MS-potilas said April 18, 2012 at 4:51 PM

@Jose Ponce Yes, this is for displaying (in randomized order) a limited count of posts, which is useful if some posts are excluded. In line:

var popcount = 8;

change the number 8 to number of posts you want.

Thank you for your compliments. :)

Jcmaster said December 5, 2015 at 12:07 AM

Hello, i want to thank you for your help, my popular posts widget was acting strange and adding posts outside the widget frame, it only holds 9 thumbnails and with this i can limit it so it wont show more posts outside its frame

Post a Comment

Related Posts Plugin for WordPress, Blogger...
See the hack
for this dynamic
views icon: