Pages

Wednesday, September 28, 2011

15

"Count" blogger posts on page

In some scripts it is be useful to know the number of posts on a page. It can be used for example to hide the "previous posts" link, I'll write about that some time later. Information about posts and their count is available only in the blog posts widget which shows the posts. First I made javascript function which incremented a counter on each post loop iteration to get the post count, but then I found out that posts widget has a template variable called data:numPosts (not documented), which holds the number of posts displayed on current page. So the code got much simpler, and that's why quotation marks for "count". Here is how you do it.

Declare javascript variable numPosts

This step is not absolutely necessary, but I like to declare the variables used in the whole page. Best place for this is in html head. So, edit template html, find </head> and add this before it:
<script type='text/javascript'>
  var numPosts = 7;
</script>  
Default value is 7 posts per page. If there is already some javascript before </head>, you can also add only the var numPosts = 7; line before the </script> tag. Something like this (highlighted line is added, others are old lines):
  // ... some old code here ...
  // ... some old code here ...

  var numPosts = 7;

//]]>
</script>
Assign actual numPosts value in the posts widget

Ok, for this one you need to edit the html, again, and select Expand Widget Templates. Then find line:
<b:includable id='main' var='top'>
and add after that these lines:
<script type='text/javascript'>
  numPosts = <data:numPosts/>;
</script>
And now we have the number of posts showing on the page in javascript variable numPosts.

Testing

To test that code works, find </body>, and add before that:
<script type='text/javascript'>
  document.title = document.title + " (viewing " + numPosts + " posts)";
</script> 
Again, if there is already some javascript there, before </body>, you can also add document.title... line there. Now when viewing the blog, window title should be "YourBlogName (viewing # posts), where # is the number of posts on page.
[Hide comments] - [Show comments]
Click on a single comment to hide/show its text

15 comments:

Mr Admin said... [reply]

hi admin, i want you help me to make script which count posts per author
thanks to answer

MS-potilas said... [reply]

@Mr Admin
Hi, I did a HTML/Javascript gadget which counts blog posts per author and display them + avatars.

Pastebin

Copy the code from pastebin (use Raw link) and put it in HTML/Javascript gadget, and see if it does what you wanted, and if it needs any improvement. :)

Mr Admin said... [reply]

Its good idea
thanks first to do it, but i want to make like author box, problem which i get that it show me all authors written posts in the blog not just the author of the read post, help me genuis MS-potilas
thanks to answer
@MS-potilas

Mr Admin said... [reply]

I suggest to create a professional author box like one on wordpress using this script and adding author Description like on profile (blogger gadget) thanks to answer me@MS-potilas

Mr Admin said... [reply]

i forget admin try to make theses script load fast thanks again@MS-potilas

MS-potilas said... [reply]

@Mr Admin Ok now I see what you are after :) I think I could do something like Wordpress author box on Blogger. I will take some time, though. I'll get back when I have something ready.

Mr Admin said... [reply]

Please, i have just one thing Mr-potilas make it easy for us to can customize it (it means in css part for avatar (.avatar{...}) for author-name (.author-name{...}) and for description (.descreaption{...}) thanks freind to respond to our request@MS-potilas

MS-potilas said... [reply]

@Mr Admin
Hi Mr Admin,

I have now a demo ready, here: http://yabtb-demo1.blogspot.fi/2012/06/author-info-box-with-post-count.html.

Texts and styles can be customized. Works on single author and multi author blogs, on single author blog it is faster as it does not have to count all posts. But it was not slow on multi author blog, either.

Author box shows only when a single post is viewed, similar to Wordpress. For the main page it would take too long time to load for every post.

If you have any thoughts at this point, tell me, I could still make some changes before I start writing an article of this author box hack. :)

Mr Admin said... [reply]

Thanks a lot MS-potilas for this good and precious work,
i have just one thing to discuss it, about description and other social links (youtube, yahoo etc...) i think here where script can load slowly with multiple authors, (i want you make it more professional it means make script call just introduction that every author write about him in his blogger profile) this thing make it too easy.
thanks to answer
@MS-potilas

Mr Admin said... [reply]

I forget an important thing about
this sentense (who has written "post number" great posts on...) it's incorrect.

if post number = 0 or 1 post without plural sign (s)
and
if post number >= 2 post take plural sign s

please include it script

thanks to ask me about my suggestions i'm very happy thanks a lot MS-potilas


@MS-potilas

MS-potilas said... [reply]

@Mr Admin
Thank you for your feedback :) I made additional code for singular and pluralis words, that is good addition. Descriptions per author can be fully customized, but in a multi user blog description can not contain scripts. In a single user blog you can put basically anything into the box. Well, hard to explain it here, I'll have to write the article.

1bungdipo said... [reply]

Great tricks. I will try to put that on my blog. I'll be back for more review.

Post a Comment

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