Create post excerpts in Blogger

One of the features sorely missing in Blogger is the ability to write an excerpt for your post (Wordpress supports several different methods), so I had to write my own JavaScript solution that provides functionality similar to the more tag in Wordpress. It hides parts of the post’s text and displays a More button which reveals the hidden text. The hidden text is enclosed within SCRIPT tags:

<script>startHide()</script>
… extra text …
<script>endHide()</script>

I’m including a JavaScript library from one of my web sites into the Blogger template. If you want to have a Blogger-only solution, include the following JavaScript code in your template:

var isMainPage = 0;
var hideCount = 0;

function dw(t) { document.write(t); }

function setMainPage() { isMainPage = 1; }

function startHide() {
hideCount ++ ;
if (!isMainPage) { dw('<div id="show_'+hideCount+'">') ; return; }
dw ('<p class="hideMenu" id="hideMenu_'+hideCount+
'"><a href="javascript:showRest('+hideCount+')">More ...</a></p>');
dw ('<div id="hide_'+hideCount+'" style="display: none;">');
}

function showRest(id) {
var e = document.getElementById('hide_'+id);
if (e && e.style) e.style.display = "" ;
e = document.getElementById('hideMenu_'+id) ;
if (e && e.style) e.style.display = "none" ;
}

function endHide() {
dw ('</div>') ;
}

3 comments:

Anonymous said...

Hi, I'm not sure what I'm doing wrong, but I get the following error from Blogger when I try to insert your code:

Your template could not be parsed as it is not well-formed. Please make sure all XML elements are closed properly.
XML error message: The entity name must immediately follow the '&' in the entity reference.

Any suggestions? =)

Ivan Pepelnjak said...

If you want to have the JavaScript in Blogger template, you have to enclose it in the <script>...</script> tags.

Rasmus said...

Like Wordpress, I want to show the first 55 words in all of my blogposts without change all of them (I have written about 200 so far)

How can I do this ? Is there a specific code I can insert in my html code ?

Post a Comment