Retrieving related posts by tags but excluding certain tags in Tumblr

I have a script on tumblr that automatically shows related posts based on the tags that the post has — the code works just fine but I would like some tags to be ignored. I’m trying to add an ‘exclude tag’ parameter which allows you to specify tags that you don’t want the script to search for. Here is my code.

(function() {
var config = new Object();
var titles = [];
var links = [];
var items = [];

var scripts = document.getElementsByTagName('script');
var this_script = scripts[scripts.length - 1];
var params = this_script.src.replace(/^[^?]+??/,'').split('&');   

var url_base = ((typeof(config.url) == 'undefined') ? ('http://' + document.domain + '/') : ('http://' + config.url + '/'));

for(var i=0; i<params.length; i++) {
    var tmp = params[i].split("=");
    config[tmp[0]] = unescape(tmp[1]);
}

document.write(
    '<div id="relposts">' +
        '<div id="relposts-loading">Loading related posts...</div>' +
        '<div id="relposts-title"></div>'+
        '<ul id="relposts-list"></ul>' +
    '</div>'
);

if(typeof(config.tags)=='undefined'){ error(0); return; }
if(typeof(config.num)=='undefined'){ config.num=8; }
if(typeof(config.len)=='undefined'){ config.len=60; }
if(typeof(config.css)=='undefined'){ document.write('<link rel="stylesheet" type="text/css" href="http://relposts.googlecode.com/svn/trunk/default.css" />');}

var tags = config.tags.slice(0,-1).split(',');

$(document).ready(function() {
    function getRelated() {
        var req;
        for(var i=0; i<tags.length; i++){
            req=$.getJSON(url_base+'api/read/json?callback=?&num='+config.num+'&start=0&tagged='+escape(tags[i]), function(data) {
                $(data.posts).each(function(i, post) {
                    var text='';
                    if(post.type=='regular') text+=post['regular-title'];
                    else if(post.type=='link') text+=post['link-text'];
                    else if(post.type=='quote') text+=post['quote-text'];
                    else if(post.type=='photo') text+=post['photo-caption'];
                    else if(post.type=='conversation') text+=post['conversation-title'];
                    else if(post.type=='video') text+=post['video-caption'];
                    else if(post.type=='audio') text+=post['audio-caption'];
                    else if(post.type=='answer') text+=post['question'];
                    if(text.length>config.len){ text=text.slice(0,config.len); text+='...';}
                    titles.push(text.replace(/(<[^<>]*>)/g, ""));
                    links.push(post['url-with-slug']);                        
                });

            }).complete(getList);
        }

    }
    function getList(){
        for(var i=0; i<titles.length; i++){
            var regex = new RegExp('('+links[i]+')');
            var html = $("#relposts-list").html();

            if(links[i]!=document.location&&!html.match(regex)){
                if(config.num--<=0) return;

                var item='<li class="relposts-item"><a class="relposts-link" href="'+links[i]+'">'+titles[i]+'</a></li>';
                $("#relposts-list").append(item);
            }
        }
        $("#relposts-title").html('Related posts:');
        $("#relposts-loading").html('');
    }
    getRelated();

});

function getError(e){
    var msg="error: ";
    switch(e){
        case 0: msg+='no tags defined'; break;
        case 1: msg+='tumblr API problem'; break;
    }
    $("#relposts-loading").html(msg);
}

I found another script that has the “excluded” tags functionality and when i tried implementing it on my script, I failed miserably. I have very little knowledge (if any) on javascript. This is the script that has what I’m looking for.

https://code.google.com/p/tumblr-related-posts/

I would like to continue using the script that I have and not that one. Could someone please lead me in the right direction? Thanks in advance for the help.


Source: css

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.