2007-07-24

Adding a Read more link to posts with specific labels

A tutorial on expandable post summaries is available on Blogger Help. If it doesn't work you might have forgotten to enclose the code with <style></style> before placing it between the </b:skin> and </head>.

Now if it works, you may find, to your disappointment, that all of your posts now have a Read more! link whether the post was truncated or not. A work-around this is by adding a snippet of code that shows the link to posts with specific labels only.

Let us say that most of your long posts are news articles under the news
label and the short posts consist only of site updates. To create expandable articles with the label news, insert the following code between <b:if cond='data:blog.pageType!= "item"'></b:if>.

<b:if cond='data:post.labels'>
<b:loop values='data:post.labels' var='label'>
<b:if cond='data:label.name == "news"'>
<br/><a expr:href='data:post.url'>Read more...</a>
</b:if></b:loop></b:if>

What the code does is that it checks if the post has a list of labels, then it goes through this list. If the label news is found in the list, it prints the link.

Follow the Blogger tutorial, use the code above instead to be placed in the body and change news to any label you want the link to appear in the posts.

Another version is to show the Read more link in posts by a specific author. To do this repalce the conditional statement above to:

<b:if cond='data:post.author== "name"'>
<br/><a expr:href='data:post.url'>Read more...</a></b:if>

Change the name to the specific author.

That's it!

2 comments:

Unknown said...

Thanks! Your tip got rid of the "read more" on every post ...

But now there's no link at all. Would you take a look at this code and tell me if I've messed up here?

[style]
[b:if cond='data:blog.pageType == "item"']
span.fullpost {display:inline;}
[b:else/]
span.fullpost {display:none;}
[/b:if]
[/style]
[head]


Further down ...

[data:post.body/]

[b:if cond='data:blog.pageType != "item"']

[b:if cond='data:post.labels']
[b:loop values='"data:post.labels"' var='"label"']
[b:if cond='data:label.name == "expand"']
[a expr:href='data:post.url']Read more...[/a]
[/b:if]
[/b:loop]
[/b:if]
[/b:if]

Thanks. Oh, and I'd be happy to link to you if that's of interest.

Dianne said...

Hi top-martyr,

Oops.. Sorry I wrote the wrong code. It should be

b:loop values='data:post.labels' var='label'

with single quotation marks and not both.

Also, I forgot the '/' in the head tag.

We'd be happy too if you'd link to us.