Thursday, January 8, 2015

Blogger. Related posts different from post title

These related posts are only text.
Frequently related posts contain same title as post title.
In this post I'll show you a way to avoid this behavior.
  1. First go to blog template and, in textbox that will appear hitting ctrl+f, insert </head> tag. Hit 'enter' to search it. Just above it insert the following code:
     <!--RelatedPostsStarts-->
      <script language='JavaScript'>
    //<![CDATA[
    var relatedTitles = new Array();
    var relatedTitlesNum = 0;
    var relatedUrls = new Array();
    function related_results_labels(json) {
    for (var i = 0; i < json.feed.entry.length; i++) {
    var entry = json.feed.entry[i];
    relatedTitles[relatedTitlesNum] = entry.title.$t;
    for (var k = 0; k < entry.link.length; k++) {
    if (entry.link[k].rel == 'alternate') {
    relatedUrls[relatedTitlesNum] = entry.link[k].href;
    relatedTitlesNum++;
    break;
    }
    }
    }
    }
    function removeRelatedDuplicates() {
    var tmp = new Array(0);
    var tmp2 = new Array(0);
    for(var i = 0; i < relatedUrls.length; i++) {
    if(!contains(tmp, relatedUrls[i])) {
    tmp.length += 1;
    tmp[tmp.length - 1] = relatedUrls[i];
    tmp2.length += 1;
    tmp2[tmp2.length - 1] = relatedTitles[i];
    }
    }
    relatedTitles = tmp2;
    relatedUrls = tmp;
    }
    function contains(a, e) {
    for(var j = 0; j < a.length; j++) if (a[j]==e) return true;
    return false;
    }
    function printRelatedLabels() {
    var r = Math.floor((relatedTitles.length - 1) * Math.random());
    var i = 0;
    if(relatedTitles.length!=0)
    document.write("<span style=\"color:#CC6600;font-size:0.9em; font-weight:bold; font-family:georgia, helvetica, sans-serif;\">Ti potrebbero interessare anche:<\/span>");
    document.write('<ul>');

    while (i < relatedTitles.length && i < 5) {
    var str=window.location.href;

    var str1=relatedUrls[r];
    var str2=str1.replace("blogspot.com","blogspot.your_country_suffix");
    if (str2!=str){
    document.write('<li><a href="' + relatedUrls[r] + '">' + relatedTitles[r] + '</a></li>');
    }

    if (relatedTitles.length-1==0){
    document.write('<font color="blue">No related posts for this post</font>');
    break;
    }
    if (r < relatedTitles.length - 1) {
    r++;
    } else {
    r = 0;
    }
    i++;
    }
    document.write('</ul>');
    }
    //]]>
    </script>

                  In above code pay attention to your contry suffix in the navbar
var str2=str1.replace("blogspot.com","blogspot.your_country_suffix");

Replace 'your_country_suffix' with, for example, 'us' if your contry is USA, and so on.


  • After, search

    <div class='post-footer-line post-footer-line-3'>
  • Just below insert

    <b:if cond='data:blog.pageType == "item"'>
    <div id='related-posts'>
    <b:loop values='data:post.labels' var='label'>
    <b:if cond='data:label.isLast != "true"'>
    </b:if>

    <script expr:src='"/feeds/posts/default/-/" + data:label.name + "?alt=json-in-script&callback=related_results_labels&max-results=5"' type='text/javascript'/>
    </b:loop><br/>
    <script type='text/javascript'>

    var currentposturl="<data:post.url/>";
    var maxresults=5; <!-- Increase To Show More Then 5 Related Post -->
    removeRelatedDuplicates(); printRelatedLabels();
    </script>
    </div>
    </b:if>
  • Done! Now you have related posts in every post of your blog.

    No comments:

    Post a Comment