Wednesday, September 27, 2006

Adding Label Feeds to Your Template

Recently I saw a post over at phydeaux3's blog explaining how to access each of your label's individual feeds.

I decided I wanted a link to the feed next to each label listed in the official Blogger Labels page element.

Here's what I did...

The first thing to do is to go to Template -> 'Page Elements' -> 'Add a Page Element', and add a label element to your template.

Then go to 'Edit HTML' and check the the checkbox for 'Expand Widget Templates'

Now you'll wanna find where the label element is located in your template. It should look something like this:

<b:widget id='Label1' locked='false' title='Labels' type='Label'>
<b:includable id='main'>
<b:if cond='data:title'>
<h2><data:title/></h2>
</b:if>
<div class='widget-content'>
<ul>
<b:loop values='data:labels' var='label'>
<li>
<b:if cond='data:blog.url == data:label.url'>
<data:label.name/>
<b:else/>
<a expr:href='data:label.url'><data:label.name/></a>
</b:if>
(<data:label.count/>)
</li>
</b:loop>
</ul>

<b:include name='quickedit'/>
</div>
</b:includable>
</b:widget>

Next, you're gonna want to add a piece of javascript:
<script type='text/javascript'>
var lblname = '<data:label.name/>';
lblname = encodeURIComponent(lblname);
var feedlink = 'http://beta.blogger.com/feeds/YOURBLOGID/posts/full/-/' + lblname;
document.write('&lt;a href=&quot;' + feedlink + '&quot;&gt;&lt;img src=&quot;http://img304.imageshack.us/img304/3518/rect18st2.png&quot;/&gt;&lt;/a&gt;');
</script>

The parts in bold are the parts you need to (or may want to) change. YOURBLOGID is obviously going to be your blog id. You can exchange summary for full if you want. You can also use a different image if you want, or even use text instead of an image.

Finally, your Label element will look something like this when you're finished:
<b:widget id='Label1' locked='false' title='Labels' type='Label'>
<b:includable id='main'>
<b:if cond='data:title'>
<h2><data:title/></h2>
</b:if>
<div class='widget-content'>
<ul>
<b:loop values='data:labels' var='label'>
<li>
<script type='text/javascript'>
var lblname = '<data:label.name/>';
lblname = encodeURIComponent(lblname);
var feedlink = 'http://beta.blogger.com/feeds/YOURBLOGID/posts/full/-/' + lblname;
document.write('&lt;a href=&quot;' + feedlink + '&quot;&gt;&lt;img src=&quot;http://img304.imageshack.us/img304/3518/rect18st2.png&quot;/&gt;&lt;/a&gt;');
</script>

<b:if cond='data:blog.url == data:label.url'>
<data:label.name/>
<b:else/>
<a expr:href='data:label.url'><data:label.name/></a>
</b:if>
(<data:label.count/>)
</li>
</b:loop>
</ul>

<b:include name='quickedit'/>
</div>
</b:includable>
</b:widget>

An example of what this looks like is located in my sidebar.


Update:
Here's an even better way to do the above (in this case better means not requiring javascript):
<b:widget id='Label1' locked='false' title='Labels' type='Label'>
<b:includable id='main'>
<b:if cond='data:title'>
<h2><data:title/></h2>
</b:if>
<div class='widget-content'>
<ul>
<b:loop values='data:labels' var='label'>
<li>
<a expr:href='"http://beta.blogger.com/feeds/YOURBLOGID/posts/full/-/" + data:label.name'><img src='http://img304.imageshack.us/img304/3518/rect18st2.png'/></a>
<b:if cond='data:blog.url == data:label.url'>
<data:label.name/>
<b:else/>
<a expr:href='data:label.url'><data:label.name/></a>
</b:if>
(<data:label.count/>)
</li>
</b:loop>
</ul>

<b:include name='quickedit'/>
</div>
</b:includable>
</b:widget>

Once again, the part in bold is what you add


Couple More Updates
Thanks to protesto (see comment below) the line in bold that gets added can be as simple as dropping in the following:
<a expr:href='data:post.url + "/feeds/posts/default/-/" + data:label.name'><img src='http://img304.imageshack.us/img304/3518/rect18st2.png'/></a>

If you prefer to show the feed image as a background image via css then you can follow Vivek Sanghi's comment posted below.

Finally, if you use special/reserved url characters in your label names then you're gonna want to use the javascript method to insert the feed image. The reasoning for this is that the javascript method uses the encodeURIComponent() method which, for example, will replace the slash character (/) with %2F and make the link point to the correct location.

18 comments:

phydeaux3 said...

Very nice!

PurpleMoggy said...

thanks

protesto said...

Great hack, thanks !

Patrick said...

beautiful

Vivek Sanghi said...

Hello Purple moggy. I learnt another way of doing this: via template's CSS.

Code in the skin tag:

a.labelfeed-link {
/* IE5.0/Win doesn't apply padding to inline elements,
so we hide these two declarations from it */
background/* */:/**/url("http://photos1.blogger.com/blogger2/2604/6063341998323/320/rss.png") no-repeat 0 .25em;
padding-left:15px;
}
html>body a.labelfeed-link {
/* Respecified, for IE5/Mac's benefit */
background:url("http://photos1.blogger.com/blogger2/2604/6063341998323/320/rss.png") no-repeat 0 .25em;
padding-left:15px;
}

The label feed link:

<a class='labelfeed-link' expr:href='"http://stubborn-fanatic.blogspot.com/feeds/posts/default/-/" + data:label.name' title='Subscribe'/>

One more thing. I noticed that the "x comments" link in your post footers does not lead directly to the comment posting page. I also had this problem (if you want to call it that) and found that no other blogs had it. So I changed mine. I think this problem happened with me because I used the code (for fixing the 1 comment bug) from a tutorial on someone's blog.

PurpleMoggy said...

Yeah, I actually changed that link so it just goes to the comments section on the permalink page instead of going directly to the post a comment page.

I never really liked that clicking on that link took you straight to this ugly brown page. Just personal preference I guess.

protesto said...

If you dont want to type your blog url on the template, you can use this default code for all templates:

a expr:href='data:post.url + "/feeds/posts/full/-/" + data:label.name'

True Life said...

Okay, the JS version takes the dash special character, but why doesn't it change a "'"?

PurpleMoggy said...

@True Life

It won't take an apostrophe because it reads it in like this:

'<data:label.name/>'

so that becomes:

''' (3 apostrophes)

which will give an error

You could change it to:

"<data:label.name/>"

but then you can't have any quotation marks in your label names

Unfortunately, there's no perfect solution here :(

Alice said...

FYI, this person is giving away templates with your hacks already added. Do they have your permission to distribute your work?

http://web-messengers.blogspot.com/2006/12/3-column-minima-templates-modded-for.html

PurpleMoggy said...

@Alice

Yeah, that's fine. As long as you give a link back I don't really care.

Alain said...

Compatibility with Drop-Down Labels Hack: Hi PurpleMoggy! So much intersting stuff on your blog! I’m trying to implement your Label Feed Hack at Techno Focus. I have Ramani's Drop-Down Labels Hack (Hackosphere) already installed on my template. This particular hack has transformed my Lablels Page Element and it doesn’t have the [li] ([ = < and ] = >) code portion anymore. Is there a way to insert your Label Hack to the Drop-Down Labels Hack? Thanks!

(hibiscus) said...

aw great right when i needed it you did the work for me! i added a [style="vertical-align: middle;"] to the [img] link for pretty.

Janna said...

Is there a way to add the label list to your page if you want to keep using the old template version of Blogger?

Shiva said...

it will be better if u give a previewof the html effect along with the coding
http://www.supershiva.blogspot.com

Gopal Aggarwal said...

thank you guys and gals, this enhancement is cool

a tip: why not use a smaller icon for Tags (and a bigger one for full posts)- Feedburner.com has a standard orange small icon here

http://www.feedburner.com/fb/lib/images/icons/feed-icon-12x12-orange.gif

I can't say if Feedburer.com would like our hotlinking to it, maybe we could store on http://imageshack.us etc

this could aid in the Reader knowing that the Label feed is a sub-section of the Full Feed

regards
gopal1035@gmail.com
India

telliham said...

hiya..

i have added this to my html.. if you go to my blog and click 'all this time' you will see... it isn't working..

any ideas what i'm missing? xx

Nicki said...

Interesting work around. I had been doing mine manually on a separate page. I like your solution better! :)