Skip to main content

Related posts

So, In the BHF we had an user who came up with this idea of having related posts link under all posts: [MaapsWorld] How to use Lambda expression. Therefore I planned to add some salt and pepper to it and write an article on it.

When it comes to related posts, first thing is that there is no way of automating this one easily. So we'll have half automation and half manual works on maintaining this gadget.

The steps in implementing this gadget is split into three,
  1. Creating posts with proper Labels.
  2. Altering XML
  3. Updating(if in case).
First of all, Back up  your template and then begin.

Creating posts with proper Labels

So blogger/blogger bot or the codes we write cannot easily identify the related posts and we have to leave a clue to find it. That is where Labels help us. Create posts with labels so as to categorize.

For example,
  1. I write all the posts that are gadget customization, css addition etc., with label named "Blogger Customizations"
  2. Similarly issues under the label "Blogger Issues"
  3. Some simple tricks under "Blogger Tricks"
In the same way, categorize your posts.

Altering XML

1. Go to blogger.com Dashboard > Themes > Edit HTML [Classic Blogger]
1. Go to blogger.com Dashboard > Themes > Options(three dots on the right) > Edit HTML [Modern Blogger]
2. In the appearing page, click randomly somewhere on the box which contains codes.
3. Press Ctrl + F and find, <data:post.body/>
4. Right after the found code, place the following code,

<!--See related posts appear only if required-->
<b:if cond='data:post.labels any (label => label.name in ["Blogger Issues"])'>
<b>See related posts:</b>
</b:if>

<!-- Start of link list-->
<ul style='text-align: left;'>

<!--If post has label Blogger Issues, this is displayed-->
<b:if cond='data:post.labels any (label => label.name == "Blogger Issues")'>
  <li><a expr:href='data:blog.homepageurl + "/search/label/Blogger%20Issues"'>Blogger Issues</a></li>
</b:if>

</ul>

5. In the above code, change the ones highlighted in green to the label you require and it's link respectively.

Updating

Well. Adding a number of label links under read more is up to your requirement. For example, if I have to add another label under recent posts, I would just add the following code,

<!--If post has label Facebook, this is displayed-->
<b:if cond='data:post.labels any (label => label.name == "Facebook")'>
  <li><a expr:href='data:blog.homepageurl + "/search/label/Facebook"'>Blogger Issues</a></li>
</b:if>

and add Facebook Label to the list of all labels and the final code will look like below,

<!--See related posts appear only if required-->
<b:if cond='data:post.labels any (label => label.name in ["Blogger Issues", "Facebook"])'>
<b>See related posts:</b>
</b:if>

<!-- Start of link list-->
<ul style='text-align: left;'>

<!--If post has label Blogger Issues, this is displayed-->
<b:if cond='data:post.labels any (label => label.name == "Blogger Issues")'>
  <li><a expr:href='data:blog.homepageurl + "/search/label/Blogger%20Issues"'>Blogger Issues</a></li>
</b:if>

<!--If post has label Facebook, this is displayed-->
<b:if cond='data:post.labels any (label => label.name == "Facebook")'>
  <li><a expr:href='data:blog.homepageurl + "/search/label/Facebook"'>Blogger Issues</a></li>
</b:if>

</ul>

and now, right under my post, you could see this widget working.

I have added a number of labels in the code so that,
  1. The post you're reading right now has Blogger Customizations & Blogger Tricks under See related posts.
  2. At the same time, this post -> Facebook OG Object Debugger has Blogger Issues & Facebook under See related Posts
  3. Whereas this post -> Backing up Template does not have the widget at all because it has no related label that I have in my code.
Oh my god, I do not see "See related posts" in some blog posts. What would I do? 
Some would see the gadget appearing but not the title, See related posts. This means that you have not added the label in the list of all labels. For example, if I add a label XML to the code and forget to add it in the list(see below code), then for the blog posts with tag XML, you'll be able to see the list of related posts but not it's title.

<!--See related posts appear only if required-->
<b:if cond='data:post.labels any (label => label.name in ["Blogger Issues", "Facebook"])'>
<b>See related posts:</b>
</b:if>

In this code, if you're not adding the new tag and it is the only tag available to that post and this code in common, you'll not see "See related posts" text showing up. To correct it, we should add the tag like below,

<!--See related posts appear only if required-->
<b:if cond='data:post.labels any (label => label.name in ["Blogger Issues", "Facebook", "XML"])'>
<b>See related posts:</b>
</b:if>


Update 2
Instead of writing multiple conditional tags, this can be written with only one loop tag. Read more here: Related posts. Looping with Multiple Labels

I hope this covers. Happy tweaking.
See related posts:

Comments

  1. Thanks for these free coding...!!!
    Can you tell me how to include more than two labels in single one coding of related post?
    Can we use coma (,) to add more than two labels in a code..... like in your case...
    Blogger Customizations ,
    Blogger Tricks

    ReplyDelete
    Replies
    1. Yes. It is possible Shekhar. Thanks for reading through. Please try using multiple labels using loop tags in XML. Read more at : Related posts. Looping with Multiple Labels

      Delete
  2. How to do it. I thought item pagetype only fetch 1 post in loop data:posts.

    ReplyDelete

Post a Comment

People also read,

How to secure your Google account and Blogger blog?

As a part of contributions to NCSAM, I am writing this article down on what all should we be aware of with respect to Google's security aspect and how to make sure your blog stays yours forever. What is NCSAM? It's National Cyber Security Awareness Month which is observed during the month of October every year in America. However, we all know cyber security is a world topic and is not limited to just the USA. So let me give some tips with respect to keeping the Google account safe and Blogger blogs safe from hackers. As I secure my account, I hope you people catch up too. In this blog post, I am going to tell about the following, Securing your Google account. Securing your Blogger blog. Securing your Google account Since Blogger is also part of your Google account, let's first see what all we need to do to keep our Google account safe. Don't share your password to anyone. Keep changing your password every 60 - 90 days. Make sure your password is str

Blogger Tweak - Open external links in new tab automatically

So, once upon a time, a really long while ago, I tried to customise and recreate a Blogger template which is not available anymore, but there's one thing I remember about it. I had a jQuery code written to make all the external links open in new tab. I also thought, it's about time I revive that idea and write a more intact code and here we go.  So, there are two things to do.  Add Javascript code that detects external links and opens them in new tab. Add a CSS code to let users know that a particular link is going to be opened in new tab. Adding a javascript code Please follow the instructions below, Go to https://blogger.com  dashboard Navigate to Themes > click the down arrow near "Customize" and then click on Edit HTML. Now scroll to the very end and paste the following code just before </body> tag. <script type='text/javascript'> document.body.addEventListener(&quot;mousedown&quot;,function(event){       if(event.target.tagNa

Blogger custom domain confusions - Check before you buy

In the Blogger community forum, I happened to help a user who was not able to publish their blogspot address to their custom domain. After doing all the debugging, there was no option left out but to contact Google and finally we have known what the problem is. The problem was that the URL was not available for use in blogger.  Here on, before spending money on buying a domain, please ensure blogger allows it. Why isn't the custom domain available to buy but not available to use on Blogger? That'd be the first question every user would ask or think to ask. Unfortunately reason is un-disclosed by Blogger themselves.  So how do we know if a domain is available at Blogger or not? Earlier, there used to be no way we could do this as Blogger would throw a more generic error like below, Now, we get a more customised error for this particular case alone like below, So, here on, before spending money on buying a domain, please try publishing your blog in the domain name you are plannin