Pages

Thursday, December 8, 2011

27

Excluding specific popular posts in Blogger

Popular posts gadget is great. But sometimes it might display entries, that you don't want to advertise (anymore). For example, some post made for testing may be popular, but there is no need to show that to users. Or you might have an old and a new version of some article in the popular posts, and you don't wish to display the old one.

When editing your blog template, you should know you way around to not to break your blog.

To exclude posts, first write down the urls of the posts you want to exclude from popular posts. Then edit template html, expand widget templates, and find:
<b:includable id='main'>
  <b:if cond='data:title'><h2><data:title/></h2></b:if>
  <div class='widget-content popular-posts'>
    <ul>
      <b:loop values='data:posts' var='post'>
Just after those lines add one line per post to be excluded, a line like this:
<b:if cond='data:post.href != &quot;http://yabtb.blogspot.com/2011/09/dummy-post-8th-post.html&quot;'>
Then find (in the same popular posts code block):
      </li>
      </b:loop>
Between those two lines, put one:
</b:if>
for each excluded posts, so if you exclude 3 posts, put 3 </b:if>s, like this:
      </li>
</b:if>
</b:if>
</b:if>
      </b:loop>

I have omitted the indentation of the added lines in purpose, so they "stand out" better and can easily be spotted and edited.

Excluding the post that user is currently viewing

Find:
<b:includable id='main'>
  <b:if cond='data:title'><h2><data:title/></h2></b:if>
  <div class='widget-content popular-posts'>
    <ul>
      <b:loop values='data:posts' var='post'>
After that add:
<b:if cond='data:post.href != data:blog.url'>
And add one </b:if> after the </li> line, like in the previous chapter.

You may want to shuffle the popular posts, too. Or maybe shuffle and limit the post count to certain number (like a random set of five posts from the ten most popular minus currently viewed post) – maybe more about that later.
[Hide comments] - [Show comments]
Click on a single comment to hide/show its text

27 comments:

Martin said... [reply]

Hi!

This is something I've been wanting to do on my blog for a while, so I was happy to find your little tutorial for it. :)
Unfortunately I get an error message when I insert the first two lines. It says:


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

Any idea on how to fix this?

Your blog is very helpful! Keep it up.

Kitos!

MS-potilas said... [reply]

@Martin
Hi, that sounds somewhat strange. Some ideas:

Check that

<b:if cond='data:post.href != &quot;http://yabtb.blogspot.com/2011/09/dummy-post-8th-post.html&quot;'>

you have changed the bold part of the above code, and nothing else (there may be multiple lines). Then double check the places where you're pasting the code, and check you insert the correct number of </b:if>s.

If you're trying to implement both excluding urls and excluding current post, try to get the exclude url(s) working before adding exclude to current post.

Hope you get it sorted.

Martin said... [reply]

@MS-potilas

Ah, I see. The Syntax Higlighter messed things up and added the (span) tag as well as making a (a class="linkclass"...) tag.

Also it seems like the Blogger interface wants " instead of ". Hehe! Got it working now though.

Thanks again! :)

Claiming Our Space said... [reply]

Is there a way to exclude all the posts in a category (i.e., they all have the same tag) utilizing the URL of the search?

Thanks.
-K

MS-potilas said... [reply]

@Claiming Our Space
Using this method, that is unfortunately not possible. The tag information is not available at that place in the template. So you should add all topics one by one.

It might be possible to exclude (actually hide) popular posts containing a specific tag using jQuery, but it would be quite complicated, I think not worth it versus adding the topics one by one.

Claiming Our Space said... [reply]

@MS-potilas
Thanks for the info, even if it isn't the answer I was hoping for. :-)

Laura @ hip pressure cooking said... [reply]

I love this, I need this. Some thoughts if you plant to release a son of this script...

I'm a little scared to always edit the template. Would love to make these changes once, and then have a hidden "widget" where I can update the URLS without changing the template.

One common post that I ALWAYS want to remove, is the latest post. Usually, its both at the top of the blog AND the top of the widget, so even just automating that (and maybe adding a number of "x latest posts") would make maintaining the items on this script simpler.

Now that, thanks to you, I can make the images round, big or small, I have another change request... Can you supress the title, too? At least for my heavy photo-centric blog it's OK to only show a photo of the most popular post. Plus, the reader may want to click on it to figure out what it is.

Thank you so much for sharing all of these little hacks, and fixes.

Ciao,

L

MS-potilas said... [reply]

@Laura @ hip pressure cooking
Hi, this technique has its drawbacks, which is the need to edit the template. With jQuery it might be possible to have the action in a gadget. Also to exclude n number of newest posts would require javascript/jQuery. Maybe I'll try to do something like that some day :)

About hiding the titles, that should be quite easy. Edit template CSS, add following definition:

.popular-posts .item-title { display:none !important; }

Laura @ hip pressure cooking said... [reply]
This comment has been removed by the author.
Laura @ hip pressure cooking said... [reply]

@Laura @ hip pressure cooking

Found the answer to my own question, deleted previous post with question

Sorry!

L

Jon Thorsen said... [reply]

I love this, but it looks like it does not replace any links you exlcude? So if I have it set to 10 and I exclude 2, it will show 8 instead of pulling in the next most popular links. Is it possible to get it to display 10 after the exclusions?
Thanks!

MS-potilas said... [reply]

@Jon Thorsen
Yes, if two posts are excluded, it will not show 10 but 8 posts. Unfortunately the list of popular posts is not available in template/scripts, so excluded items cannot be replaced.

Ky Ell said... [reply]

Thank you! Been wondering how to do this for a while!

Rakesh Kumar said... [reply]

Sir, can we exclude a post from home page. I mean I have a complete lists of posts that I want to hide from the regular loop on the home page. Will be gald to know about the process. Thanking you, Rakesh

MS-potilas said... [reply]

@Rakesh Kumar
Hi,
I did some testing, and yes, it can be done. I will make an article about it when I have time, sometime this week, I think.

DoujinOST said... [reply]

Does this work on labels? I would like to hide all post that has "some" label

MS-potilas said... [reply]

@DoujinOST Hi, unfortunately this approach - excluding posts on template level - won't work with labels, because there is no label information available from the popular posts. It might be possible to hide the posts afterwards using javascript/jQuery, but that would require quite much coding and the end result would be slower. I'd personally just put the posts one by one in the template like described in this hack.

jerome mahieux said... [reply]

Hi,
I NEVER manage to obtain a True result when performing a test on an url string like you explain here :



if is always False even if the post.url or blog.url strings match..

I did not made my blog public yet.. could it be a reason ?

also I found a trick about the problem I have using javascript from here :

http://translate.google.com/translate?sl=fr&tl=en&js=n&prev=_t&hl=fr&ie=UTF-8&u=http%3A%2F%2Fstephane-rouilly.blogspot.fr%2F2013%2F12%2Fsurligner-les-titres-du-menu-de-votre.html

this works but well..

jerome mahieux said... [reply]

sorry you filter html gt and lt are removed below.
the test i wrote is similar to yours :

b:if cond='data:post.url == "http://xxx/p/contact.html"'
(located in a posts loop)

b:if cond='data:blog.url == "http://xxx/p/contact.html"'
(located almost anywhere)

in any case I can retrieve the url strings :
data:blog.url/
data:post.url/

when the strings match b:if remains False

MS-potilas said... [reply]

@jerome mahieux
Hi,
one thing comes to my mind... You have to use the blogspot.com as domain in the urls you compare, so don't use "national" urls like blogspot.co.uk, blogspot.fr, blogspot.in, etc. Always .com. Hope you get it working.

JimmyDee said... [reply]

Could you not perhaps use another field? Perhaps the author tag? Enable another author that is yourself, then set a flag for the author? Is that available at the template page?

MS-potilas said... [reply]

@JimmyDee
Hi,
that could be possible, but I'm not sure how easy it is to change the author of a post. Here are the layout tags that can be used https://support.google.com/blogger/answer/47270?hl=en#posts, look under posts. The tag that is used must be directly under the posts (so labels, which is a list, won't work).

Lara B. said... [reply]

Dear MS-potilas,
Thank you for sharing how to do this! :) Your instructions were very clear and worked very well!
Now if only there was a way to exclude posts with certain labels from appearing as Popular Posts, but I just read your answer in the comments that there is not. My problem is that giveaways tend to be very popular posts, so as I gradually exclude them, my Popular Posts sidebar will show fewer and fewer posts. :P ;) :D I'm just adding smileys at this point so I can see how they convert in your comments. A very cool feature! LOL

Subhodeep Mukhopadhyay said... [reply]

Dear MS-potilas,
I am currently redesigning my website and your post has been very helpful. I used to be a programmer long ago, and wasn't very confident I'd be able to edit html code after such a long time.

But your clear and crisp instructions made it very easy for me to implement your code and hide some older irrelevant posts.

I am now inspired to try some other hacks also! :)

Thanks & Warm Regards

Bukulawas said... [reply]

I need to exclude some posts with specific label from appearing in the Popular Post widget. I've tried to change the code from :

b:if cond='data:post.href != "http://yabtb.blogspot.com/2011/09/dummy-post-8th-post.html"'

to this.. but it's not working as I expected..

b:if cond='data:post.label.name != "MY_EXCLUDED_POST_LABEL"'

any suggestion..? Thanks...

MS-potilas said... [reply]

@Bukulawas
Hi, unfortunately this approach - excluding posts on template level - won't work with labels, because there is no label information available from the popular posts. It might be possible to hide the posts afterwards using javascript/jQuery, but that would require quite much coding and the end result would be slower. I'd personally just exclude the posts one by one in the template like described in this hack.

Post a Comment

Related Posts Plugin for WordPress, Blogger...
See the hack
for this dynamic
views icon: