我不是程序员,所以也许我在某个地方寻找的信息,但我不知道如何搜索它。
我有这个脚本按类别发布最近的帖子。它将永远得到该类别的最新发布帖子而不会结束。如果有任何解决方案让它运行一次。
示例:如果我将此代码放入博客文章中,我希望它仅发布来自特定类别的最新5个帖子,并在此之后停止从该类别发布任何新发布的帖子。
我不知道你是否理解我的意思,但请原谅我,因为我不是程序员。
谢谢..
<style>
/*Recent Post By tag*/
img.recent-thumb{float:left;margin-right:10px;height:50px;width:50px;}
.recent-by-tag{width:100%;margin:0;padding:0}
ul.recent-by-tag li{padding:10px 0;margin-bottom:0;margin-right:28px;border-bottom:1px solid #ddd}
.recent-by-tag li{font-size:13px;list-style:none;padding-left:0;margin:0;padding:0;overflow:hidden}
.recent-by-tag a{font-family:'Oswald',sans-serif;font-size:14px;font-weight:normal;margin:0;}
.recent-by-tag strong{padding-left:0}
span.showdates{font-size:14px;margin:10px 0 0}</style>
<div id="rcentDiv">
</div>
<script type='text/javascript'>
var numposts = 10;
var showpostthumbnails = true;
var showpostdate = false;
// Recent Post By Tag
// Recent Post By Tag For Blogger
// insert revised rcentbytag() code here
function rcentbytag(e) {
var storedHTML = "";
function saveHTML(h) {
storedHTML += h;
}
saveHTML('<table><tbody><tr><td><ul class="recent-by-tag">');
for (var t = 0; t < numposts; t++) {
var n = e.feed.entry[t];
var r = n.title.$t;
var i;
if (t == e.feed.entry.length) break;
for (var o = 0; o < n.link.length; o++) {
if (n.link[o].rel == "replies" && n.link[o].type == "text/html") {
var u = n.link[o].title;
var f = n.link[o].href
}
if (n.link[o].rel == "alternate") {
i = n.link[o].href;
break
}
}
var l;
try {
l = n.media$thumbnail.url
} catch (h) {
s = n.content.$t;
a = s.indexOf("<img");
b = s.indexOf('src="', a);
c = s.indexOf('"', b + 5);
d = s.substr(b + 5, c - b - 5);
if (a != -1 && b != -1 && c != -1 && d != "") {
l = d
} else l = "http://2.bp.blogspot.com/-giova1ZCh-A/Uzq6L8QTJNI/AAAAAAAAAJc/USXictTq_xs/s70-c/KM+Icon.png"
}
var p = n.published.$t;
var v = p.substring(0, 4);
var m = p.substring(5, 7);
var g = p.substring(8, 10);
var y = new Array;
y[1] = "January";
y[2] = "February";
y[3] = "March";
y[4] = "April";
y[5] = "May";
y[6] = "June";
y[7] = "July";
y[8] = "August";
y[9] = "September";
y[10] = "October";
y[11] = "November";
y[12] = "December";
saveHTML('<li class="clear">');
if (showpostthumbnails == true) saveHTML('<a href="' + i + '" target ="_blank" title="' + r + '"><img class="recent-thumb" alt="' + r + '" src="' + l + '"/></a>');
saveHTML('<strong><a href="' + i + '" target ="_blank" title="' + r + '">' + r + '</a></strong>');
saveHTML('<br>');
var x = "";
var T = 0;
if (showpostdate == true) {
x = '<span class="showdates">' + x + g + " " + y[parseInt(m, 10)] + " " + v + "</span>";
T = 1
}
saveHTML(x);
saveHTML("</li>");
if (t != numposts - 1) saveHTML("")
}
saveHTML("</ul></td></tr></tbody></table>")
document.getElementById("rcentDiv").innerHTML = storedHTML;
}
</script>
<script async="async" src="https://www.example.com/feeds/posts/default/-/CategoryName?orderby=published&alt=json-in-script&callback=rcentbytag" return false;"></script>
答案 0 :(得分:0)
使用布尔值(是/否值)检查函数是否已经运行。
var CodeRan;
function YourFunction(){
if(!CodeRan){ // If CodeRan is false...
// Set CodeRan to true, so the next time the function is called it wont get past the If statement.
CodeRan = true;
// Your code goes here.
} // End of IF statement.
} // End of yourFunction
答案 1 :(得分:0)
用这个替换你的所有代码,让我知道它是否有效;更改页面后,请确保您刷新页面(谷歌浏览器中的ctrl + F5)以获取新内容。请注意,您提供的代码中存在许多不良做法,我在这里只进行了轻微编辑;如果这是一个生产网站,我强烈建议雇用熟悉现代JavaScript技术的开发人员,如ES6模块。
<style>
/*Recent Post By tag*/
img.recent-thumb{float:left;margin-right:10px;height:50px;width:50px;}
.recent-by-tag{width:100%;margin:0;padding:0}
ul.recent-by-tag li{padding:10px 0;margin-bottom:0;margin-right:28px;border-bottom:1px solid #ddd}
.recent-by-tag li{font-size:13px;list-style:none;padding-left:0;margin:0;padding:0;overflow:hidden}
.recent-by-tag a{font-family:'Oswald',sans-serif;font-size:14px;font-weight:normal;margin:0;}
.recent-by-tag strong{padding-left:0}
span.showdates{font-size:14px;margin:10px 0 0}</style>
<div id="rcentDiv">
</div>
<script type='text/javascript'>
var limitreached = false;
var maxposts = 5;
var numposted = 0;
var showpostthumbnails = true;
var showpostdate = false;
// Recent Post By Tag
// Recent Post By Tag For Blogger
// insert revised rcentbytag() code here
function rcentbytag(e) {
if (limitreached === false) {
var storedHTML = "";
function saveHTML(h) {
storedHTML += h;
}
saveHTML('<table><tbody><tr><td><ul class="recent-by-tag">');
for (var t = 0; t < maxposts; t++) {
numposted = numposted + 1;
if (numposted > maxposts) {
limitreached = true;
}
if (limitreached === false) {
var n = e.feed.entry[t];
var r = n.title.$t;
var i;
if (t == e.feed.entry.length) break;
for (var o = 0; o < n.link.length; o++) {
if (n.link[o].rel == "replies" && n.link[o].type == "text/html") {
var u = n.link[o].title;
var f = n.link[o].href
}
if (n.link[o].rel == "alternate") {
i = n.link[o].href;
break
}
}
var l;
try {
l = n.media$thumbnail.url
} catch (h) {
s = n.content.$t;
a = s.indexOf("<img");
b = s.indexOf('src="', a);
c = s.indexOf('"', b + 5);
d = s.substr(b + 5, c - b - 5);
if (a != -1 && b != -1 && c != -1 && d != "") {
l = d
} else l = "http://2.bp.blogspot.com/-giova1ZCh-A/Uzq6L8QTJNI/AAAAAAAAAJc/USXictTq_xs/s70-c/KM+Icon.png"
}
var p = n.published.$t;
var v = p.substring(0, 4);
var m = p.substring(5, 7);
var g = p.substring(8, 10);
var y = new Array;
y[1] = "January";
y[2] = "February";
y[3] = "March";
y[4] = "April";
y[5] = "May";
y[6] = "June";
y[7] = "July";
y[8] = "August";
y[9] = "September";
y[10] = "October";
y[11] = "November";
y[12] = "December";
saveHTML('<li class="clear">');
if (showpostthumbnails == true) {
saveHTML('<a href="' + i + '" target ="_blank" title="' + r + '"><img class="recent-thumb" alt="' + r + '" src="' + l + '"/></a>');
}
saveHTML('<strong><a href="' + i + '" target ="_blank" title="' + r + '">' + r + '</a></strong>');
saveHTML('<br>');
var x = "";
var T = 0;
if (showpostdate == true) {
x = '<span class="showdates">' + x + g + " " + y[parseInt(m, 10)] + " " + v + "</span>";
T = 1
}
saveHTML(x);
saveHTML("</li>");
if (t != maxposts - 1) {
saveHTML("");
}
}
}
saveHTML("</ul></td></tr></tbody></table>");
document.getElementById("rcentDiv").innerHTML = storedHTML;
}
}
</script>
<script async="async" src="https://www.example.com/feeds/posts/default/-/CategoryName?orderby=published&alt=json-in-script&callback=rcentbytag" return false;"></script>