12 Elegant Post Summaries for Blogger with jQuery
Thursday, September 4, 2008 /

The ability to display extracts of posts is a feature which many of us would love to see added to Blogger! Such a feature would prevent readers having to scroll far down the page to find the next post on home and archive pages, and in many cases, could offer the potential for different styles of layout...



Many Blogger users have adopted Ramani's selective expandable posts hack or a similar alternative. However, I have found these to be a little complicated: they require manual editing of each post or cause other irritating problems. In my search for an elegant and less cumbersome alternative, I have discovered an almost perfect solution: the jQuery Expander Plugin by Karl Swedberg.









Integrating the jQuery Expander plugin for Blogger offers many useful options and features:

  • It can be added easily in only two steps!

  • The length (and other options) of excerpts is completely customizable within the blog template (no need to edit JavaScript files!)


  • We could choose to expand posts into the same page (with a smooth transition) or add a "read full post" link to the item page instead.

  • HTML tags and complete words are preserved (unlike many forms of Wordpress excerpts)

  • There is no requirement for additional tags or editing of posts: it works right away after installation!

  • It's possible to use different lengths of extracts for use in magazine style layouts (I'm actually working on such a project right now...)

To see how well the jQuery expander plugin could work in Blogger, take a look at this demonstration in which the posts expand into the same page.

Installation

Depending on the style of post summaries you would prefer to display, the method for installation will be slightly different.



I'll begin with a default explanation which will summarize posts with a link to expand in the same page (this requires only two steps). Next we can make two small changes which prevent expansion and add a link to the post page.



Finally I'll explain how you can alter some options for extracts which can be applied to both methods.



For whichever function you prefer, the installation is simple and the result will be elegant post summaries for your Blogger powered blog :)





Method 1: Expandable post summaries



This is the simplest method to add excerpt functionality to your blogger powered blog!



Firstly, go to Layout>Edit HTML in your Blogger dashboard and check the "expand widget templates" box.



Then, find the closing </head> tag in your blog template, and paste the following section of code immediately before it:





<script language='javascript' src='http://halotemplates.s3.amazonaws.com/jquery-truncator/jquery-1.2.3.pack.js' type='text/javascript'/>

<script language='javascript' src='http://halotemplates.s3.amazonaws.com/jquery-truncator/jquery.expander.js' type='text/javascript'/>

<script type='text/javascript'>

$(document).ready(function() {



$('.excerpt').expander({

slicePoint: 280, // default is 100

expandText: '[...]', // default is 'read more...'

});



});

</script>


Next, find this section of code (or similar) in your template:





    <div class='post-body entry-content'>

      <data:post.body/>

      <div style='clear: both;'/> <!-- clear for photos floats -->

    </div>


If you cannot easily find this section, search for the following tag using your browser's search function:



<data:post.body/>



You need to highlight this and any surrounding code, as in this screenshot:





Replace this section of code with the following instead:





  <b:if cond='data:blog.pageType != "item"'>

    <div class='excerpt post-body entry-content'>

      <data:post.body/>

      <div style='clear: both;'/> <!-- clear for photos floats -->

    </div>

  <b:else/>

    <div class='post-body entry-content'>

      <data:post.body/>

      <div style='clear: both;'/> <!-- clear for photos floats -->

    </div>

</b:if>


Now preview your template. You should now notice that posts on your home page have been summarized and feature an ellipsis at the point of truncation which (once viewed in the live page) is a link to expand/truncate the post:



If you're happy with this, you can save your template and enjoy using your new post summaries right away. Alternatively, read on for methods of preventing the expansion in the same page, and customizing the excerpts to suit your preferences.





Method 2: Excerpts with a link to the post page



My personal preference is to display only an excerpt of posts with a link to the full article. This requires only two small changes to the default installation:

  • Use a slightly different jQuery plugin (change the URL)

  • Add a "Continue reading" link beneath the excerpts

Assuming you have already added the codes from Method 1, here is what you should do:



1. Find this line in the <head> section of your template:



<script language='javascript' src='http://halotemplates.s3.amazonaws.com/jquery-truncator/jquery.expander.js' type='text/javascript'/>



And replace it with this instead:



  <script language='javascript' src='http://halotemplates.s3.amazonaws.com/jquery-truncator/jquery.extractor.js' type='text/javascript'/>



I have highlighted the different file name in bold red so you can see the change at a glance.



2. Next, find the code you have added around the <data:post.body/> section, and add the line in bold red, as shown below:

  <b:if cond='data:blog.pageType != "item"'>

    <div class='excerpt post-body entry-content'>

      <data:post.body/>

      <div style='clear: both;'/> <!-- clear for photos floats -->

    </div>

    <a expr:href="data:post.url">Continue Reading</a>

  <b:else/>

    <div class='post-body entry-content'>

      <data:post.body/>

      <div style='clear: both;'/> <!-- clear for photos floats -->

    </div>

</b:if>
Be careful to add this in the correct place, just above the <b:else/> tag, otherwise it will not be displayed!



Now preview your template. The "linked" ellipsis should now be replaced with a plain one, and you should see the "Continue reading" link appear beneath the extract. You could change the link text to anything you prefer (eg: "Read more", "Full Post", etc). Then proceed to save your template.





Post Summary Options

The jQuery plugins used for these methods are almost identical, except that the "extracts" version has the "more" link removed.



Both plugins therefore have identical options which you can set by adding or changing code in your blog template.



The options can be configured within the internal javascript in the head section which by default appears like this:





<script type='text/javascript'>

$(document).ready(function() {



$('.excerpt').expander({

slicePoint: 280, // default is 100

expandText: '[...]', // default is 'read more...'

});



});

</script>




Here are some of the most used options available, and how they can be changed. A full list of options can be found on the expander plugin for jQuery page.







<script type='text/javascript'>

$(document).ready(function() {



  $('.excerpt').expander({

    slicePoint:       280,  // change this to the length in characters you would like to display.

                            // HTML tags included in this count.

    // the folllowing functions apply when choosing the expanding post version

    expandEffect:     'fadeIn', // the effect used for displaying expanded text.

    expandText:         '[...]', // Change this from an ellipsis to "Read More" for example

    userCollapseText: '[^]'  // This is the icon used for truncating the post after it has been expanded.

  });

 

});

</script>


You can add or change any of the lines above as appropriate for your needs.





License, Credits and Download

This function uses the jQuery JavaScript library and the jQuery expander plugin for the base functions.



The Expander Plugin for jQuery was created by Karl Swedberg and is dual licensed under the MIT and GPL licenses.



If you would prefer to host the jQuery scripts on your own server, you can download a zipped folder including all scripts from this tutorial and a basic Minima template in which this function has been added for demonstration or testing purposes. As in the case of the original Expander Plugin and jQuery file, these are provided under the GPL licence.



Download Elegant Blogger Post Summaries Files



I hope you have found this tutorial to be a useful and elegant solution for creating and using post summaries for your Blogger template. Please feel free to leave your comments and any suggestions for improvement below.



12 comments:

February 5, 2009 at 2:12 AM dom said...

I too have the 'undefined' error. After I expand my post it says 'undefined' in place of the original expanding link.

If use
(a class="read-more" href="#")Click to read story(/a)(span class="details")
in my posts [with < and > instead of ( and )] I don't get the error any more. But I do get a few other annoying problems and defeat the point of this great plugin. Although a number of people seem to have this problem, no one seems to have figured it out.

February 8, 2009 at 7:34 PM dom said...

Why oh why is undefined there? I have tried everything I can think of. I have tried every solution on this page.

BTW, Thanks for the fix SPM. Two problems; the link is already clicked when you load the page and the link shows up in my feeds.

Thanks for the code tho, seems my best bet.

I have looked around the web. I'm about to give up on jquery; it's great but that undefined error is killing me. So close to being perfect . . .

February 8, 2009 at 7:48 PM dom said...

www.concisebahamasnews.com is my blog, sorry for not posting before. as you can see, this code was a big part of it.

Great tutorial and site btw. sorry for not mentioning before.

April 1, 2009 at 9:29 PM Alek Tomanov said...

Very helpful tutorial again thank you Amanda!

April 11, 2009 at 6:57 PM VeNoM said...

I have used the jquery but at times it gives me a warning saying that the script cannot be loaded or the host server amazonaws is busy and hence cannot process your script.

Is there any substitute to this query or can I host the query on some other site viz. googlepages? If so i need the code/script! How to go about this?

Thanx Amanda for the jquery! :-)

April 14, 2009 at 8:02 AM
Gurujee said...

hi Amanda, Thanks for the tutorial. I was searching for it. And it works like a charm.

The only thing that I want is that "Continue Reading" link just at the end of dots..
http://i41.tinypic.com/2v17yhf.png

Can it be done ?

May 14, 2009 at 5:30 AM Rica said...

Hi Amanda,

I've been wanting to use the expandable summaries instead of the second option but it never works for any of my layouts. I checked the code and the (') signs turns into numbers. :(

I prefer that over the second option because when I edit my posts (ex: making a text bold or italic) I always have to fix it because the "/span" comes out and it ruins the summary cut. Is there a way to fix this?

May 20, 2009 at 8:39 PM ed hardy said...

There are ed hardy shirts
,pretty ed hardy shirt for men, ed hardy womens in the ed hardy online store designed by ed hardy ,many cheap ed hardy shirt ,glasses,caps,trouers ed hardy shirts on sale ,
You can go to edhardyshirts.com to have a look ,you may find one of ed hardy clothing fit for you
http://straighthairgirl.blog126.fc2.com
http://www.seriousblogging.com/crazygirlsshirts
http://www.free-blog-site.com/iammyself
http://d.hatena.ne.jp/hotfishing
chaussure pumachaussure sport chaussure nike

May 20, 2009 at 8:40 PM ed hardy said...

There are ed hardy shirts
,pretty ed hardy shirt for men, ed hardy womens in the ed hardy online store designed by ed hardy ,many cheap ed hardy shirt ,glasses,caps,trouers ed hardy shirts on sale ,
You can go to edhardyshirts.com to have a look ,you may find one of ed hardy clothing fit for you
http://straighthairgirl.blog126.fc2.com
http://www.seriousblogging.com/crazygirlsshirts
http://www.free-blog-site.com/iammyself
http://d.hatena.ne.jp/hotfishing
chaussure pumachaussure sport chaussure nike

July 7, 2009 at 8:03 PM Mario Mendoza said...

This is a tricky tips but very useful to those who runs blogger blog like me. It's a brilliant idea. Great Job. Thanks for this post. Gems Collection

September 7, 2009 at 8:41 PM thisurbanjungle said...

Great! Fixed the "undefined"! Thanks!

Post a Comment