The ability to 'paginate' Blogger blogs has been a long requested feature. Pagination allows us to number blog pages like those of a book, offering readers the ability to navigate deeper into the archives with ease when compared to the 'newer' and 'older' post links.
After many failed attempts, I was almost ready to admit defeat with this function until I discovered this excellent script by Mohamed of Blogger Accessories. This script replaces the default blog pager (links to newer, older and home pages) with numbered page navigation. Furthermore, it offers the ability to customize the number of page links and the number of posts displayed on each page.
Mohamed's Blogger pagination script may be installed using two different methods:
- Customizing the template code
- Adding as an HTML/JavaScript widget (gadget) below the main posts section
Both of these installation methods will achieve a similar effect. I personally find adding the code directly to the template makes it easier to customize the style of the pagination links, but if you feel uncomfortable editing your template, the widget installation may be much easier for you.
Here is how the finished installation will appear in your blog:
Once installed, these "page numbers" will appear on your home, archive and labels pages, allowing your readers to easily access pages in your archives. However, the page numbers will not appear on individual post pages. I've noticed many blogs which use pagination like this, and among them none displayed these page links beneath individual posts.
To learn more about using the numbered pagination script for Blogger, pop over to Blogger Accessories to read the useful tutorials Mohamed has posted.
My Slightly Modified Version
As you may have noticed, I'm using a slightly modified version of this script here on Blogger Buster. When customizing the style of Mohamed's original version, I noticed a few elements which I preferred to change for my own preferences, and also modified the CSS for how the numbered links are displayed.
If you would prefer to use this slightly different version instead, here is what you should do.
Add the CSS code to your style section
Firstly, copy all of the code below and paste this just before the closing</b:skin>
tag in your template: This should change the colors of text and links in the pagination to match the styles already present in your blog template. So if your default text is white against a dark background, the "Pages (#)" section of the script should appear the same way. The color for the background and hover effects of the numbers can be altered according to your preferences by editing the hex values to match the style of your own template.1: .showpageArea a {
2: text-decoration:underline;
3: }
4: .showpageNum a {
5: text-decoration:none;
6: border: 1px solid #ccc;
7: margin:0 3px;
8: padding:3px;
9: }
10: .showpageNum a:hover {
11: border: 1px solid #ccc;
12: background-color:#ccc;
13: }
14: .showpagePoint {
15: color:#333;
16: text-decoration:none;
17: border: 1px solid #ccc;
18: background: #ccc;
19: margin:0 3px;
20: padding:3px;
21: }
22: .showpageOf {
23: text-decoration:none;
24: padding:3px;
25: margin: 0 3px 0 0;
26: }
27: .showpage a {
28: text-decoration:none;
29: border: 1px solid #ccc;
30: padding:3px;
31: }
32: .showpage a:hover {
33: text-decoration:none;
34: }
35: .showpageNum a:link,.showpage a:link {
36: text-decoration:none;
37: color:#333;
38: }
Add the JavaScript code beneath your posts
The second part of the installation is to add the JavaScript code beneath your posts which will locate and replace the existing "Newer/Older Posts" link. To add this script, go to Layout>Edit HTML in your Blogger dashboard, and do not tick the "expand widget templates" box (it's much easier to add the code while the widgets are contracted). Then find this section of code (or similar) in your template:
1: <b:section class='main' id='main' showaddelement='yes'> <b:widget id='Blog1' locked='false' title='Blog Posts' type='Blog'/> </b:section>
Immediately after the closing </b:section>
tag, add the following section of code:
1: <script type="text/javascript">
2: function showpageCount(json) {
3: var thisUrl = location.href;
4: var htmlMap = new Array();
5: var isFirstPage = thisUrl.substring(thisUrl.length-5,thisUrl.length)==".com/";
6: var isLablePage = thisUrl.indexOf("/search/label/")!=-1;
7: var isPage = thisUrl.indexOf("/search?updated")!=-1;
8: var thisLable = isLablePage ? thisUrl.substr(thisUrl.indexOf("/search/label/")+14,thisUrl.length) : "";
9: thisLable = thisLable.indexOf("?")!=-1 ? thisLable.substr(0,thisLable.indexOf("?")) : thisLable;
10: var thisNum = 1;
11: var postNum=1;
12: var itemCount = 0;
13: var fFlag = 0;
14: var eFlag = 0;
15: var html= '';
16: var upPageHtml ='';
17: var downPageHtml ='';
18: var pageCount = 5;
19: var displayPageNum = 5;
20: var upPageWord = 'Previous';
21: var downPageWord = 'Next';
22: var labelHtml = '<span class="showpageNum"><a href="/search/label/'+thisLable+'?&max-results='+pageCount+'">';
23: for(var i=0, post; post = json.feed.entry[i]; i++) {
24: var timestamp = post.published.$t.substr(0,10);
25: var title = post.title.$t;
26: if(isLablePage){
27: if(title!=''){
28: if(post.category){
29: for(var c=0, post_category; post_category = post.category[c]; c++) {
30: if(encodeURIComponent(post_category.term)==thisLable){
31: if(itemCount==0 || (itemCount % pageCount ==(pageCount-1))){
32: if(thisUrl.indexOf(timestamp)!=-1 ){
33: thisNum = postNum;
34: }
35: postNum++;
36: htmlMap[htmlMap.length] = '/search/label/'+thisLable+'?updated-max='+timestamp+'T00%3A00%3A00%2B08%3A00&max-results='+pageCount;
37: }
38: }
39: }
40: }//end if(post.category){
41: itemCount++;
42: }
43: }else{
44: if(title!=''){
45: if(itemCount==0 || (itemCount % pageCount ==(pageCount-1))){
46: if(thisUrl.indexOf(timestamp)!=-1 ){
47: thisNum = postNum;
48: }
49: if(title!='') postNum++;
50: htmlMap[htmlMap.length] = '/search?updated-max='+timestamp+'T00%3A00%3A00%2B08%3A00&max-results='+pageCount;
51: }
52: }
53: itemCount++;
54: }
55: }
56: for(var p =0;p< htmlMap.length;p++){
57: if(p>=(thisNum-displayPageNum-1) && p<(thisNum+displayPageNum)){
58: if(fFlag ==0 && p == thisNum-2){
59: if(thisNum==2){
60: if(isLablePage){
61: upPageHtml = labelHtml + upPageWord +'</a></span>';
62: }else{
63: upPageHtml = '<span class="showpage"><a href="/">'+ upPageWord +'</a></span>';
64: }
65: }else{
66: upPageHtml = '<span class="showpage"><a href="'+htmlMap[p]+'">'+ upPageWord +'</a></span>';
67: }
68: fFlag++;
69: }
70: if(p==(thisNum-1)){
71: html += '<span class="showpagePoint">'+thisNum+'</span>';
72: }else{
73: if(p==0){
74: if(isLablePage){
75: html = labelHtml+'1</a></span>';
76: }else{
77: html += '<span class="showpageNum"><a href="/">1</a></span>';
78: }
79: }else{
80: html += '<span class="showpageNum"><a href="'+htmlMap[p]+'">'+ (p+1) +'</a></span>';
81: }
82: }
83: if(eFlag ==0 && p == thisNum){
84: downPageHtml = '<span class="showpage"> <a href="'+htmlMap[p]+'">'+ downPageWord +'</a></span>';
85: eFlag++;
86: }
87: }//end if(p>=(thisNum-displayPageNum-1) && p<(thisNum+displayPageNum)){
88: }//end for(var p =0;p< htmlMap.length;p++){
89: if(thisNum>1){
90: if(!isLablePage){
91: html = ''+upPageHtml+' '+html +' ';
92: }else{
93: html = ''+upPageHtml+' '+html +' ';
94: }
95: }
96: html = '<div class="showpageArea"><span style="COLOR: #000;" class="showpageOf"> Pages ('+(postNum-1)+')</span>'+html;
97: if(thisNum<(postNum-1)){
98: html += downPageHtml;
99: }
100: if(postNum==1) postNum++;
101: html += '</div>';
102: if(isPage || isFirstPage || isLablePage){
103: var pageArea = document.getElementsByName("pageArea");
104: var blogPager = document.getElementById("blog-pager");
105: if(postNum <= 2){
106: html ='';
107: }
108: for(var p =0;p< pageArea.length;p++){
109: pageArea[p].innerHTML = html;
110: }
111: if(pageArea&&pageArea.length>0){
112: html ='';
113: }
114: if(blogPager){
115: blogPager.innerHTML = html;
116: }
117: }
118: }
119: </script>
120: <script src="/feeds/posts/summary?alt=json-in-script&callback=showpageCount&max-results=99999"; type="text/javascript"></script>
Once you have added this code, attempt to preview your template to ensure there are no code errors. You will not be able to see the script in action on the preview, but this step ensures no adverse effects to your layout.
In the code above, you will notice that I have highlighted three sections of code in bold red. You may need to customize these sections to better suit your preferences:
On line 5, you may need to change the ".com" to reflect your domain extention if you use a custom domain.
1: var isFirstPage = thisUrl.substring(thisUrl.length-5,thisUrl.length)==".com/";
This default installation should work fine for anyone hosted on Blog*Spot or a dot.com domain, but you will need to alter this section if your custom domain ends in .co.uk or .info for example.
In lines 19 and 20, you can change these values to alter how many posts are displayed on each page, and how many links will be visible at the same time.
The
var pageCount = 5;
section defiines the number of posts displayed on each page, while var displayPageNum = 5;
defines the number of links which are displayed (eg: 1 - 2 - 3 - 4 - 5) beneath the posts section.Once you have added this JavaScript code and made any changes nescessary for your needs, save your template and open up your blog in your browser. Now you should see the pagination links appear beneath your main posts section, and also on Labels and Archive pages.
From : BloggerBuster
No comments:
Post a Comment