How do you show only posts with a particular label on your blogger homepage? Read on. What if you want to write a blog with a primarily technical audience, but also want to be able to write complete cruft and not have to bother readers with it? I searched for how to show only posts with a particular label on the home page but the closest thing I found was this post asking how to remove content from the homepage with a particular label. Of course, that post had no responses. So I wrote my own solution, which turned out to be easier than I expected. This could easily work to support inclusive or exclusive filtering. I haven't been able to find much documentation on the markup for Blogger (if you know where it is, please let me know) but I was able to find some documentation in a few blogs such as the excellent Hoctro's Place. That post gave me enough information to put this hack together. The only thing you really need to know is that you can create a 'function' with includable and you can call it with include. From your blog dashboard, click on "Layout" then click on "Edit HTML". Once you have done that, click the "Expand Widget Templates" checkbox. Now you're ready to edit. As always, I recommend making a backup before you get started. First, search for the line that starts with <b:includable id='main' var='top'>. You are going to remove everything in that function between <b:loop values='data:posts' var='post'> and the closing tag with the following:
<b:if cond='data:blog.url == data:blog.homepageUrl'> <b:if cond='data:post.labels'> <b:loop values='data:post.labels' var='label'> <b:if cond='data:label.name == "main"'> <b:include data='post' name='printPosts'/> </b:if> </b:loop> </b:if> <b:else/> <b:include data='post' name='printPosts'/> </b:if>The above change essentially says the following: if you are on the homepage, and a post has labels, and the post has the "main" label, then call printPosts. Or, if you are not on the homepage, call printPosts. Now search for the close tag of the main function (it looks like </b:includable>) and after it paste the following code:
<b:includable id='printPosts' var='post'> <b:if cond='data:post.dateHeader'> <h2 class='date-header'><data:post.dateHeader/></h2> </b:if> <b:include data='post' name='post'/> <b:if cond='data:blog.pageType == "item"'> <b:include data='post' name='comments'/> </b:if> </b:includable>The above code is your printPosts function/method. It is identical to what you had before, it was just turned into a function to reduce code duplication. That's it. Now only posts with a "main" label will show up on the front page. For Peter Sachs, who was wanting to filter out content, just change the (== "main") markup to something like (!= "nsfw"). Enjoy.

12 comments:
Ouch ! This is definitely not for dummies... My template does not even have a b:includable tag to begin with...
I forgot to mention you have to click the box that says, "Expand Widget Templates". Let me know if that helps and I'll update the post.
OK, works like a charm (that really was a dummy question all right ;-)
Thanks a lot !!
Glad it worked for you, I have updated the post.
Tried this hack to exclude a label from the main page, and it worked!...But...it quadrupled all of my other posts.
Where did I go wrong?
http://fishnetburnns.blogspot.com
Not sure what you mean. I looked at your blog and everything seems normal. Can you send me a URL with an example?
maybe he mean by this condition:
he set the homepage to show 7 latest post and the first 3 post is label 'a' and the following 4 post is label 'b'. he got another 5 post for label 'a' by navigating on older post link. he wanted to show only label 'a' and he try your hack. but the homepage show only first 3 post and following 5 post for the same label from older post page is not showing up.
another mean is:
can we just loop the post with label 'a' rather than filter it out on homepage?
sorry for the long story. :p
Holy Filtering Batman! This actually worked! Thank you thank you thank you!!!!!
Stumbling this and bookmarking it!
if you use more than one label on any post when you use != you get duplicates on the home page. Any way to fix this?
I'd rather not have to label all my posts to filter on the main page but rather filter out the few that don't belong!!!
This was exactly what I was looking for! Thank you!
Thanks for posting this! The template code has changed if you're now using the new Blogger Template Designer, but I figured it out. The same basic idea still works, although the implementation is more complex.
thanks it working on me!
Post a Comment