如何从相关文章中排除当前 post,即使它具有相同的标签?
How to exclude the current post from related articles even though it has the same label?
我在我的博主博客中使用以下代码来显示基于标签的相关文章:
<div id='related-posts'>
<b:if cond='data:post.labels'>
<b:loop values='data:post.labels' var='label'>
<b:if cond='data:label.isLast == "true"'>
<data:label.name/>
</b:if>
</b:loop>
</b:if>
</div>
问题是当前的post显然是因为有标签而被收录在相关文章中。我想排除当前的 post,但我不知道该怎么做。
我认为这是 JavaScript 的:
$("#related-posts").each(function() {
var v = $(this).text();
$.ajax({
url: "/feeds/posts/default/-/" + v + "?alt=json-in-script&max-results=" + related_number,
type: 'get',
dataType: "jsonp",
success: function(e) {
var u = "";
var h = '<div class="related-wrap">';
for (var i = 0; i < e.feed.entry.length; i++) {
for (var j = 0; j < e.feed.entry[i].link.length; j++) {
if (e.feed.entry[i].link[j].rel == "alternate") {
u = e.feed.entry[i].link[j].href;
break
}
}
var g = e.feed.entry[i].title.$t;
var s = e.feed.entry[i].category[0].term;
var y = e.feed.entry[i].author[0].name.$t;
var c = e.feed.entry[i].content.$t;
var $c = $('<div>').html(c);
if (c.indexOf("//www.youtube.com/embed/") < -1) {
var p = e.feed.entry[i].media$thumbnail.url.replace('/default.jpg', '/mqdefault.jpg');
var k = p
} else if (c.indexOf("<img") > -1) {
var q = $c.find('img:first').attr('src').replace('s72-c', 's1600');
var k = q
} else {
var k = no_image
}
h += '<li><div class="related-thumb"><a class="related-img" href="' + u + '" style="background:url(' + k + ') no-repeat center center;background-size: cover"><span class="related-overlay"/></a></div><div class="related-content"><div class="related-tag"><a class="icon ' + s + '" href="/search/label/' + s + '">' + s + '</a></div><h3 class="related-title"><a href="' + u + '">' + g + '</a></h3></div></li>'
}
h += '</div><div class="clear"/>';
$("#related-posts").html(h);
$(this).find('.related-img').each(function() {
$(this).attr('style', function(i, src) {
return src.replace('/default.jpg', '/mqdefault.jpg')
}).attr('style', function(i, src) {
return src.replace('s72-c', 's1600')
})
})
}
})
});
将data-postId
添加到html代码
<div id='related-posts' expr:data-postId='data:view.postId'>
<b:if cond='data:post.labels'>
<b:loop values='data:post.labels' var='label'>
<b:if cond='data:label.isLast == "true"'>
<data:label.name/>
</b:if>
</b:loop>
</b:if>
</div>
然后在您的 Javascript 代码中再添加几行
$("#related-posts").each(function() {
var v = $(this).text();
// 1) get the current post id
var postId = $(this).attr('data-postId');
$.ajax({
url: "/feeds/posts/default/-/" + v + "?alt=json-in-script&max-results=" + related_number,
type: 'get',
dataType: "jsonp",
success: function(e) {
var u = "";
var h = '<div class="related-wrap">';
for (var i = 0; i < e.feed.entry.length; i++) {
// 2) skip if it's the current post
if(e.feed.entry[i].id.$t.match(/\d+$/)[0] === postId){
continue
}
for (var j = 0; j < e.feed.entry[i].link.length; j++) {
if (e.feed.entry[i].link[j].rel == "alternate") {
u = e.feed.entry[i].link[j].href;
break
}
}
var g = e.feed.entry[i].title.$t;
var s = e.feed.entry[i].category[0].term;
var y = e.feed.entry[i].author[0].name.$t;
var c = e.feed.entry[i].content.$t;
var $c = $('<div>').html(c);
if (c.indexOf("//www.youtube.com/embed/") < -1) {
var p = e.feed.entry[i].media$thumbnail.url.replace('/default.jpg', '/mqdefault.jpg');
var k = p
} else if (c.indexOf("<img") > -1) {
var q = $c.find('img:first').attr('src').replace('s72-c', 's1600');
var k = q
} else {
var k = no_image
}
h += '<li><div class="related-thumb"><a class="related-img" href="' + u + '" style="background:url(' + k + ') no-repeat center center;background-size: cover"><span class="related-overlay"/></a></div><div class="related-content"><div class="related-tag"><a class="icon ' + s + '" href="/search/label/' + s + '">' + s + '</a></div><h3 class="related-title"><a href="' + u + '">' + g + '</a></h3></div></li>'
}
h += '</div><div class="clear"/>';
$("#related-posts").html(h);
$(this).find('.related-img').each(function() {
$(this).attr('style', function(i, src) {
return src.replace('/default.jpg', '/mqdefault.jpg')
}).attr('style', function(i, src) {
return src.replace('s72-c', 's1600')
})
})
}
})
});
我在我的博主博客中使用以下代码来显示基于标签的相关文章:
<div id='related-posts'>
<b:if cond='data:post.labels'>
<b:loop values='data:post.labels' var='label'>
<b:if cond='data:label.isLast == "true"'>
<data:label.name/>
</b:if>
</b:loop>
</b:if>
</div>
问题是当前的post显然是因为有标签而被收录在相关文章中。我想排除当前的 post,但我不知道该怎么做。
我认为这是 JavaScript 的:
$("#related-posts").each(function() {
var v = $(this).text();
$.ajax({
url: "/feeds/posts/default/-/" + v + "?alt=json-in-script&max-results=" + related_number,
type: 'get',
dataType: "jsonp",
success: function(e) {
var u = "";
var h = '<div class="related-wrap">';
for (var i = 0; i < e.feed.entry.length; i++) {
for (var j = 0; j < e.feed.entry[i].link.length; j++) {
if (e.feed.entry[i].link[j].rel == "alternate") {
u = e.feed.entry[i].link[j].href;
break
}
}
var g = e.feed.entry[i].title.$t;
var s = e.feed.entry[i].category[0].term;
var y = e.feed.entry[i].author[0].name.$t;
var c = e.feed.entry[i].content.$t;
var $c = $('<div>').html(c);
if (c.indexOf("//www.youtube.com/embed/") < -1) {
var p = e.feed.entry[i].media$thumbnail.url.replace('/default.jpg', '/mqdefault.jpg');
var k = p
} else if (c.indexOf("<img") > -1) {
var q = $c.find('img:first').attr('src').replace('s72-c', 's1600');
var k = q
} else {
var k = no_image
}
h += '<li><div class="related-thumb"><a class="related-img" href="' + u + '" style="background:url(' + k + ') no-repeat center center;background-size: cover"><span class="related-overlay"/></a></div><div class="related-content"><div class="related-tag"><a class="icon ' + s + '" href="/search/label/' + s + '">' + s + '</a></div><h3 class="related-title"><a href="' + u + '">' + g + '</a></h3></div></li>'
}
h += '</div><div class="clear"/>';
$("#related-posts").html(h);
$(this).find('.related-img').each(function() {
$(this).attr('style', function(i, src) {
return src.replace('/default.jpg', '/mqdefault.jpg')
}).attr('style', function(i, src) {
return src.replace('s72-c', 's1600')
})
})
}
})
});
将data-postId
添加到html代码
<div id='related-posts' expr:data-postId='data:view.postId'>
<b:if cond='data:post.labels'>
<b:loop values='data:post.labels' var='label'>
<b:if cond='data:label.isLast == "true"'>
<data:label.name/>
</b:if>
</b:loop>
</b:if>
</div>
然后在您的 Javascript 代码中再添加几行
$("#related-posts").each(function() {
var v = $(this).text();
// 1) get the current post id
var postId = $(this).attr('data-postId');
$.ajax({
url: "/feeds/posts/default/-/" + v + "?alt=json-in-script&max-results=" + related_number,
type: 'get',
dataType: "jsonp",
success: function(e) {
var u = "";
var h = '<div class="related-wrap">';
for (var i = 0; i < e.feed.entry.length; i++) {
// 2) skip if it's the current post
if(e.feed.entry[i].id.$t.match(/\d+$/)[0] === postId){
continue
}
for (var j = 0; j < e.feed.entry[i].link.length; j++) {
if (e.feed.entry[i].link[j].rel == "alternate") {
u = e.feed.entry[i].link[j].href;
break
}
}
var g = e.feed.entry[i].title.$t;
var s = e.feed.entry[i].category[0].term;
var y = e.feed.entry[i].author[0].name.$t;
var c = e.feed.entry[i].content.$t;
var $c = $('<div>').html(c);
if (c.indexOf("//www.youtube.com/embed/") < -1) {
var p = e.feed.entry[i].media$thumbnail.url.replace('/default.jpg', '/mqdefault.jpg');
var k = p
} else if (c.indexOf("<img") > -1) {
var q = $c.find('img:first').attr('src').replace('s72-c', 's1600');
var k = q
} else {
var k = no_image
}
h += '<li><div class="related-thumb"><a class="related-img" href="' + u + '" style="background:url(' + k + ') no-repeat center center;background-size: cover"><span class="related-overlay"/></a></div><div class="related-content"><div class="related-tag"><a class="icon ' + s + '" href="/search/label/' + s + '">' + s + '</a></div><h3 class="related-title"><a href="' + u + '">' + g + '</a></h3></div></li>'
}
h += '</div><div class="clear"/>';
$("#related-posts").html(h);
$(this).find('.related-img').each(function() {
$(this).attr('style', function(i, src) {
return src.replace('/default.jpg', '/mqdefault.jpg')
}).attr('style', function(i, src) {
return src.replace('s72-c', 's1600')
})
})
}
})
});