gpt4 book ai didi

javascript - 如何在 Blogger 中通过 json 检索位置

转载 作者:行者123 更新时间:2023-11-30 14:39:38 25 4
gpt4 key购买 nike

我在 Blogger 中使用此脚本通过 JSON 显示带标签的帖子,正如您通过这段代码看到的,我可以检索帖子的标题、缩略图和日期等

 <script type='text/javascript'>
//<![CDATA[
function labelthumbs(json) {
document.write('<ul id="label_with_thumbs">');
for (var i = 0; i < numposts; i++) {
var entry = json.feed.entry[i];
var posttitle = entry.title.$t;
var posturl;
if (i == json.feed.entry.length) break;
for (var k = 0; k < entry.link.length; k++) {
if (entry.link[k].rel == 'replies' && entry.link[k].type == 'text/html') {
var commenttext = entry.link[k].title;
var commenturl = entry.link[k].href;
}
if (entry.link[k].rel == 'alternate') {
posturl = entry.link[k].href;
break;
}
}
var thumburl;
try {
thumburl = entry.media$thumbnail.url;
} catch (error) {
s = entry.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 != "")) {
thumburl = d;
} else thumburl = 'https://1.bp.blogspot.com/-etyXYLaEXP8/WrmiznwRzXI/AAAAAAAAAnQ/Pu4OVqzx_4kmgwnmxJGDuf6j-l6ARuHbgCLcBGAs/s1600/nothumbnailimage.png';
}
var postdate = entry.published.$t;
var cdyear = postdate.substring(0, 4);
var cdmonth = postdate.substring(5, 7);
var cdday = postdate.substring(8, 10);
var monthnames = new Array();
monthnames[1] = "January";
monthnames[2] = "February";
monthnames[3] = "March";
monthnames[4] = "April";
monthnames[5] = "May";
monthnames[6] = "June";
monthnames[7] = "July";
monthnames[8] = "August";
monthnames[9] = "September";
monthnames[10] = "October";
monthnames[11] = "November";
monthnames[12] = "December";
document.write('<li>');
if (showpostthumbnails == true)
document.write('<a class="posturl" href="' + posturl + '" target ="_top"><img class="label_thumb" src="' + thumburl + '"/></a>');
document.write('<a class="posturl" href="' + posturl + '" target ="_blank"><h3 class="posttitle">' + posttitle + '</h3><span class="dateposted">' + monthnames[parseInt(cdmonth, 10)] + ' ' + cdday + ', ' + cdyear + '></span></a>');
if ("content" in entry) {
var postcontent = entry.content.$t;
} else
if ("summary" in entry) {
var postcontent = entry.summary.$t;
} else var postcontent = "";
var re = /<\S[^>]*>/g;
postcontent = postcontent.replace(re, "");
if (showpostsummary == true) {
if (postcontent.length < numchars) {
document.write('');
document.write(postcontent);
document.write('');
} else {
document.write('');
postcontent = postcontent.substring(0, numchars);
var quoteEnd = postcontent.lastIndexOf(" ");
postcontent = postcontent.substring(0, quoteEnd);
document.write(postcontent + '...');
document.write('');
}
}
var towrite = '';
var flag = 0;
if (showpostdate == true) {
flag = 1;
}
if (showcommentnum == true) {
if (flag == 1) {
towrite = towrite + ' | ';
}
if (commenttext == '1 Comments') commenttext = '1 Comment';
if (commenttext == '0 Comments') commenttext = 'No Comments';
commenttext = '<a href="' + commenturl + '" target ="_top">' + commenttext + '</a>';
towrite = towrite + commenttext;
flag = 1;;
}
if (displaymore == true) {
if (flag == 1) flag = 1;
}
document.write(towrite);
document.write('</li>');
if (displayseparator == true)
if (i != (numposts - 1))
document.write('');
}
document.write('</ul>');
}
//]]>
</script>

我的问题是,我博客中的每篇文章都有位置名称,我可以选择指定它,例如:美国等。

那么如何通过 JSON 检索位置呢?

P.S 这是出现在我的博客模板中的位置名称的 HTML

<span class='published-location' expr:title='data:post.location.name'><data:post.location.name/></span>

最佳答案

通过帖子编辑器输入的位置数据可通过键 georss$featurename 在 JSON 提要中获得。您必须将其包含在您的代码中

..
var entry = json.feed.entry[i];
var posttitle = entry.title.$t;
<b>var location = entry.georss$featurename.$t;</b>
var posturl;
..

然后你通过上面代码中的document.write语句打印到页面上-

document.write('Location: ' + location + '<br/><a class="posturl" href="' + posturl + '" target ="_blank"><h3 class="posttitle">' + posttitle + '</h3><span class="dateposted">' + monthnames[parseInt(cdmonth, 10)] + ' ' + cdday + ', ' + cdyear + '></span></a>');

关于javascript - 如何在 Blogger 中通过 json 检索位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49870026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com