Wednesday, August 5, 2009

A Better Read More Option

In a previous post I presented a simple technique to add a Read More option to hide a desired portion of long blog posts. But that method has a drawback because it shows the "Read More" link even for the posts that do not have hidden parts.

Here's an improved version of that technique which shows the Read More link only if you have decided to hide part of a post. [Note that the "Read More" link will be displayed only on the blog home page and archive (i.e. label) pages]

1) Go to Layout -> Edit HTML and click the ‘Expand Widget Templates’ check-box.

2) Locate the </head> tag and copy the code shown in red in the following snippet, above that tag.

<b:if cond='data:blog.pageType == "item"'>
<style>span.fullpost {display:inline;}</style>
<script>function tbgHideReadMores() {}</script>
<style>span.fullpost {display:none;}</style>
function tbgHideReadMores() {
var els = document.getElementsByTagName(&#39;*&#39;);
for (i = 0; i &lt; els.length; i++) {
if ((els[i].className == &#39;post-body&#39;) || (els[i].className == &#39;post-body entry-content&#39;)) {

function tbgHideReadMore(post) {
var spans = post.getElementsByTagName(&#39;span&#39;);
var found = 0;
for (var i = 0; i &lt; spans.length; i++) {
if (spans[i].className == &quot;fullpost&quot;) {
spans[i].style.display = &#39;none&#39;;
found = 1;

var lnks = post.getElementsByTagName(&#39;a&#39;);
for (var i = 0; i &lt; lnks.length; i++) {
if ((lnks[i].innerHTML == &quot;Read More&quot;) &amp;&amp; (found == 0)){
lnks[i].style.display = &#39;none&#39;;


3) Locate the <body> tag and modify it as shown below.

<body onload='tbgHideReadMores()'>

4) Locate the <data:post.body/> element and add the code shown in red below.

<div class='post-body entry-content'>
<b:if cond='data:blog.pageType != "item"'><br/>
<a expr:href='data:post.url'>Read More</a>


5) Then for each post that you want to display a summary, you have to add the following piece of code enclosing the part that you want to hide. (Do this from the Edit Html tab of the post editor)

This is a sample long post. Only these two lines are visible on the homepage.

<span class="fullpost">This sentence, which is enclosed inside the HTML code
shown in red, will be displayed only when a visitor clicks on the Read More

  • Instead of "Read More", if you want some other phrase such as "Continue...", then you have to put that exact string in the places in which "Read More" appears in steps (2) and (4).
  • The unnecessary Read More links get hidden only after the page is fully loaded. So you might see them initially, until the home page gets fully loaded in the browser.