Blogger: Filter posts by label on your main page

I spent quit a while phishing around google trying to figure out how to prevent posts with a certain label from appearing on my main page over at blogger/blogspot. Eventually, I used an adaptation of this method (also detailed here).

To applay this method, edit your blogger sites XML template (be sure to back it up first!), and click "Expand Widget Templates". Now look for the line:

<b:loop values='data:posts' var='post'>

And find the matching </b:loop> tag.

Replace them and everything between them with the following code:

<b:loop values='data:posts' var='post'>
<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.isLast == &quot;true&quot;'>
<b:if cond='data:label.name != "Label_To_Filter">
<b:include data='post' name='printPosts'/>
</b:if>
</b:if>
</b:loop>
</b:if>
<b:else/>
<b:include data='post' name='printPosts'/>
</b:if>
</b:loop>

Now look for the last </b:includable> tag you can find, and paste this code directly after it:

<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>

Note that this code has a few bugs:

  1. Posts with no label will not be displayed on the main page.

  2. The filtered label must be the last label of a filtered post.

  3. If all your recent stories belong to the filtered category, your blog may appear empty.


I consider these "Features" as they suit my need over at www.luxphile.com - preventing any of my Texture labeled posts from showing up on the main page, and also hiding unlabeled "Blog this" posts from Flickr.

24 comments:

  1. Hey man, this was a lifesaver. I used a modified version of it to make two columns of posts on the main page. The left column has the "left" tag and so on.

    ReplyDelete
  2. Filtering Blogger Posts by Labels on Homepage

    I'm looking for something similar, but in the other direction: to only show posts with a certain combination of labels on the home page, eg: "question" + "featured".... I guess that would look something like this? (Very new to blogger customization - today, actually ; )


    b:loop values='data:posts' var='post'
    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 = "Question"
    b:if cond='data:label.name = "Featured"
    b:include data='post' name='printPosts'/
    /b:if
    /b:if
    /b:loop
    /b:if
    /b:if
    /b:loop

    ReplyDelete
  3. Hi, I'm hoping you could help me. I would love to make this work on my blog, but when I try to save the template edits, I get this message:

    XML error message: The value of attribute "cond" associated with an element type "null" must not contain the '<' character.

    Not sure what it means. I am using one of the new blogger standard templates with some customizations (background, widgets, bannner).

    Any advice would be really appreciated!!

    Thanks!

    ReplyDelete
  4. I get the same error message as tupelodogg. Doesn't work.

    ReplyDelete
  5. In case tupelodogg and motmaitre are still working with this the error is the result of a missing apostrophe. In the first block of code

    ReplyDelete
  6. Ah, it removed the code. Anyway the line with 'Label_to_Filter' in red, at the end of that, between the double quotes and the greater than sign should be a single quote

    ReplyDelete
  7. @Economic Perspectives From Kansas City - That helped so much, because I was getting that same error.

    However, now nothing is showing up and I only have one post in that "filtered" category so it's not like I have them all with it.

    Not sure what went wrong here...

    SEO Domain Names

    ReplyDelete
  8. In case tupelodogg and motmaitre are still working with this the error is the result of a missing apostrophe. In the first block of code

    kapıda ödemeli alışveriş

    ReplyDelete
  9. should i replace all codes with or replace the first block only? coz i find several codes start with

    ReplyDelete
    Replies
    1. *with b:loop values='data:posts' var='post'

      Delete
  10. I am getting this error:Error parsing XML, line 2320, column 2: The value of attribute "cond" associated with an element type "b:if" must not contain the '<' character.

    Please help!

    ReplyDelete
  11. Is there any alternative of this as of now.. not working on me..

    Please Help
    JelBee.com

    ReplyDelete
  12. I just gave this a shot, and now no posts are visible on my Home page or on my link pages. :(

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. Woops that comment got messed up I pasted it here check it out:

    http://justpaste.it/ftc8

    ReplyDelete
    Replies
    1. Thanks a lot for this! I couldn't get the code from the original post to work but your code worked on the first try. Really appreciate it.

      Delete
    2. OMG, thanks a lot Stunna, i was searching for this code more than a month, and just your, just your code really works without destroy my template.. really thanks

      Delete
  15. Thanks you very much. I applied on my blog.

    ReplyDelete
  16. Can you help me pls, I want to filter more than one label

    ReplyDelete

  17. Do you need a quick long or short term loan with a relatively low interest rate as low as 3%? We offer business loan, personal loan, home loan, auto loan,student loan, debt consolidation loan e.t.c. no matter your credit score. We are guaranteed in giving out financial services to our numerous clients all over world. With our flexible lending packages, loans can be processed and transferred to the borrower within the shortest time possible, contact our specialist for advice and finance planning. If you need a quick loan contact us via Email: williamsmr504@gmail.com

    ReplyDelete
  18. BENEFITS GIVEN TO NEW
    MEMBERS WHO WILL JOIN
    FREEMASON & ILLUMINATI.
    A
    1.Cash Reward of USD $
    980,000
    2.A New Sleek Dream CAR
    valued at USD $850,000
    3.A Dream House bought in the
    country of your own choice
    One Month holiday (fully
    paid) to your dream tourist
    destination.
    One year Golf Membership
    package
    4.A V.I.P treatment in all
    Airports in the World
    5.A total Lifestyle change
    Access to Bohemian Grove
    One Month booked
    Appointment with Top 5
    world Leaders and Top 5
    Celebrities in the World.
    call this number to join +2348134548904 or email on illuminatiorderworlwide@gmail.com

    ReplyDelete