0

Moving on from my previous question. Same little project. Different inquiry altogether.

Here's the updated code:

<script type="text/javascript">
function recentpostslist(json) {
 document.write('<ul>');
 var i;
 var j;
 for (i = 0; i < json.feed.entry.length; i++)
 {
  for (j = 0; j < json.feed.entry[i].link.length; j++) {
   if (json.feed.entry[i].link[j].rel == 'alternate') {
    break;
   }
  }
var postUrl = "'" + json.feed.entry[i].link[j].href + "'";//bs
var postTitle = json.feed.entry[i].title.$t;
var item = "<h2>" + '<a href="' + postUrl + '" target="_blank">' + postTitle + "</a> </h2>";
 document.write(item);
 }
 document.write('</ul>');
 }
</script>
<script src="https://xxxxxxxxxx.blogspot.com/feeds/posts/summary/-/recommended?max-results=3&alt=json-in-script&callback=recentpostslist"></script>

What it does is list the titles of a blog's 3 latest posts that have been labeled "recommended".

I figured I might declare another variable, just above the var item definition, as in...

var postContent = json.feed.entry[i].content.$t;

...and add that to the 'var item' value, as in...

var item = "<h2>" + '<a href="' + postUrl + '" target="_blank">' + postTitle + "</a> </h2> <p>" + postContent + "</p>";

...or something like that; my intention being to include posts' content (not just title) to what's being displayed.

But that doesn't seem to work. Am I missing something?

Community
  • 1
  • 1
m.a.a.
  • 289
  • 1
  • 11
  • 1
    What is your goal? Where are you stuck? The question shouldm't be: `Read this question first.` – Tân Jan 19 '17 at 10:32
  • I'm checking the json data of an example blog and I'm not seeing "content" anywhere. I don't think the recentpostlist json you're requesting contains it at all. –  Jan 19 '17 at 10:39
  • @TânNguyễn My previous question was just asking for a bit of clarification concerning a specific line of code. I'm just referring to it for the sake of providing some background info. This is an altogether different question. I'm describing what I'm doing in order to include post content to what's being displayed (which if I'm not mistaken is declared within the `var item` definition), I'm failing and asking what I'm doing wrong. Anyways, I'll try to edit the question a bit to suit your taste... – m.a.a. Jan 19 '17 at 10:39
  • Also note that you should clearly see `TypeError: json.feed.entry[0].content is undefined` in the browser console, pointing you right at the problem. –  Jan 19 '17 at 10:47
  • @ChrisG I read [this](http://www.mybloggertricks.com/2015/10/extract-post-excerpt-via-json.html) and [this](http://www.danpros.com/2013/08/blogger-json-feed-api). I guess there's something I've not understood... but I'd like to... – m.a.a. Jan 19 '17 at 10:47
  • You are requesting this: `https://xxxxxxxxxx.blogspot.com/feeds/posts/summary/-/recommended?max-results=3&alt=json-in-script&callback=recentpostslist` Just paste it in the browser's address bar and you can the json data. You'll find it doesn't contain the post content. You need to use a feed URL that also returns the posts' contents. –  Jan 19 '17 at 10:51
  • @Chris Thank you very much. I changed the `postContent` definition from `...content.$t` to `...summary.$t` and it works. I though those articles I was reading were talking about stuff that were included in the json.feed data by default. How come there is no `entry[i].content.$t;` in mine? – m.a.a. Jan 19 '17 at 11:10

1 Answers1

1

The URL you're using contains the word summary; if you use default instead, the json data will also contain the content, apparently.