Popsessed
06-18-2011, 07:54 PM
Hello, I got this code for my blog to show related links under each post:
(function() {
var config = new Object();
var titles = [];
var links = [];
var images = [];
var items = [];
var $j = jQuery.noConflict()
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; }
var tags = config.tags.slice(0,-1).split(',');
$j(document).ready(function() {
function getRelated() {
var req;
for(var i=0; i<tags.length; i++){
req=$j.getJSON(url_base+'api/read/json?callback=?&num='+config.num+'&start=0&type=photo&tagged='+escape(tags[i]), function(data) {
$j(data.posts).each(function(i, post) {
titles.push(post['photo-caption']);
links.push(post['url-with-slug']);
images.push(post['photo-url-100']);
});
}).complete(getList);
}
}
function getList(){
for(var i=0; i<titles.length; i++){
var regex = new RegExp('('+links[i]+')');
var html = $j("#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]+'"><img src="'+images[i]+'">'+titles[i]+'</a></li>';
$j("#relposts-list").append(item);
}
}
$j("#relposts-title").html('<h2>Some Related Images:</h2>');
$j("#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;
}
$j("#relposts-loading").html(msg);
}
})();
I love it, but it's only flaw is it works fine on individual post pages, but on the index page all the results appear under the first post, and all other posts links just keep 'loading'.
Is there any simple way to edit the script so it works fine for multiple posts on one page as well?
(function() {
var config = new Object();
var titles = [];
var links = [];
var images = [];
var items = [];
var $j = jQuery.noConflict()
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; }
var tags = config.tags.slice(0,-1).split(',');
$j(document).ready(function() {
function getRelated() {
var req;
for(var i=0; i<tags.length; i++){
req=$j.getJSON(url_base+'api/read/json?callback=?&num='+config.num+'&start=0&type=photo&tagged='+escape(tags[i]), function(data) {
$j(data.posts).each(function(i, post) {
titles.push(post['photo-caption']);
links.push(post['url-with-slug']);
images.push(post['photo-url-100']);
});
}).complete(getList);
}
}
function getList(){
for(var i=0; i<titles.length; i++){
var regex = new RegExp('('+links[i]+')');
var html = $j("#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]+'"><img src="'+images[i]+'">'+titles[i]+'</a></li>';
$j("#relposts-list").append(item);
}
}
$j("#relposts-title").html('<h2>Some Related Images:</h2>');
$j("#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;
}
$j("#relposts-loading").html(msg);
}
})();
I love it, but it's only flaw is it works fine on individual post pages, but on the index page all the results appear under the first post, and all other posts links just keep 'loading'.
Is there any simple way to edit the script so it works fine for multiple posts on one page as well?