1
votes

The code below is for listing blogger posts within a Label Name But it lists only the title of the post, I want to know if it is possible to display the post image together, and also stylize with css, and how do I do that? In /feeds/posts/summary/-/NAME?alt=json&callback=processPostList12&start-index=" you can change the label name and will only show the posts associated with label name.

I changed the code to create class and change the position of the image to the beginning of the line, but now it is no longer in alphabetical order, I tried everything I know and could not fix it. Is it also possible that the image directs to the post? just like the title

This is the part that I've changed:

          a1E.href = url;
          a1E.textContent = title;
          a1E.className += "aclass";
          postImage.src = imageThumb;
          postImage.className += "imclass";

  var startIndex = 1;
  var maxResults = 150;
  var allResults = [];
  function sendQuery12()
  {
    var scpt = document.createElement("script");
    scpt.src = "/feeds/posts/summary/-/Series?alt=json&callback=processPostList13&start-index=" + startIndex + "&max-results=" + maxResults;
    document.body.appendChild(scpt);
  }
  function printArrayResults(root)
  { 
    //Sort Alphebetically
    allResults.sort(function(a, b){
      var a_string = a.children[0].textContent ;
      var b_string = b.children[0].textContent ;
      if(a_string < b_string) return -1;
      if(a_string > b_string) return 1;
      return 0;
    })
    var elmt = document.getElementById("postList13");
    for (index = 0; index < allResults.length; index++) {
      elmt.appendChild(allResults[index]);
    }
  }
  function processPostList13(root)
  {   
    var elmt = document.getElementById("postList13");
    if (!elmt)
      return;
    var feed = root.feed;
    if (feed.entry.length > 0)
    {
      for (var i = 0; i < feed.entry.length; i++)
      {
        var entry = feed.entry[i];
        var title = entry.title.$t;
        var date = entry.published.$t;

        if( entry.media$thumbnail != undefined ){
          var imageThumb = entry.media$thumbnail.url ;
        } else {
          var imageThumb = 'https://i.imgur.com/PqPqZQN.jpg' ;
        }

        for (var j = 0; j < entry.link.length; j++)
        {
          if (entry.link[j].rel == "alternate")
          {
            var url = entry.link[j].href;
            if (url && url.length > 0 && title && title.length > 0)
            {
              var liE = document.createElement("li");
              var a1E = document.createElement("a");
              var postImage = document.createElement("img");

              a1E.href = url;
              a1E.textContent = title;
              a1E.className += "aclass";
              postImage.src = imageThumb;
              postImage.className += "imclass";

              liE.appendChild(postImage);
              liE.appendChild(a1E);



              //elmt.appendChild(liE);
              allResults.push(liE);

            }
            break;
          }
        }
      }
      if (feed.entry.length >= maxResults)
      {
        startIndex += maxResults;
        sendQuery12();
      } else {
        printArrayResults();
      }
    }
  }
  sendQuery12();
<div class="postlist1" id="postList13">
</div>
1

1 Answers

1
votes

1- Retrieving post image from APi

after these lines:

var entry = feed.entry[i];
var title = entry.title.$t;
var date  = entry.published.$t;

you could use:

var imageThumb = entry.media$thumbnail.url;

it'll retrieve the first image thumbnail of the post. you should validate it because posts with no images will return undefined for media$thumbnail and will cause javascript error. so :

if( entry.media$thumbnail != undefined ){
    var imageThumb = entry.media$thumbnail.url;
} else {
    var imageThumb = 'alternative_image_path' ;
}

2- Appending post image :

...
var postImage= document.createElement("img");
postImage.src = imageThumb;
liE.appendChild(postImage);
...

The final code should be like that :

<div>
  <ul id="postList12"></ul>
</div>
<script type="text/javascript">
  var startIndex = 1;
  var maxResults = 150;
  var allResults = [];
  function sendQuery12()
  {
    var scpt = document.createElement("script");
    scpt.src = "/feeds/posts/summary/-/NAME?alt=json&callback=processPostList12&start-index=" + startIndex + "&max-results=" + maxResults;
    document.body.appendChild(scpt);
  }
  function printArrayResults(root)
  { 
    //Sort Alphebetically
    allResults.sort(function(a, b){
      var a_string = a.children[0].textContent ;
      var b_string = b.children[0].textContent ;
      if(a_string < b_string) return -1;
      if(a_string > b_string) return 1;
      return 0;
    })
    var elmt = document.getElementById("postList12");
    for (index = 0; index < allResults.length; index++) {
      elmt.appendChild(allResults[index]);
    }
  }
  function processPostList12(root)
  {   
    var elmt = document.getElementById("postList12");
    if (!elmt)
      return;
    var feed = root.feed;
    if (feed.entry.length > 0)
    {
      for (var i = 0; i < feed.entry.length; i++)
      {
        var entry = feed.entry[i];
        var title = entry.title.$t;
        var date = entry.published.$t;

        if( entry.media$thumbnail != undefined ){
          var imageThumb = entry.media$thumbnail.url ;
        } else {
          var imageThumb = 'https://i.imgur.com/PqPqZQN.jpg' ;
        }

        for (var j = 0; j < entry.link.length; j++)
        {
          if (entry.link[j].rel == "alternate")
          {
            var url = entry.link[j].href;
            if (url && url.length > 0 && title && title.length > 0)
            {
              var liE = document.createElement("li");
              var a1E = document.createElement("a");
              var postImage = document.createElement("img");

              a1E.href = url;
              a1E.textContent = title;
              postImage.src = imageThumb;

              liE.appendChild(a1E);
              liE.appendChild(postImage);

              //elmt.appendChild(liE);
              allResults.push(liE);

            }
            break;
          }
        }
      }
      if (feed.entry.length >= maxResults)
      {
        startIndex += maxResults;
        sendQuery12();
      } else {
        printArrayResults();
      }
    }
  }
  sendQuery12();
</script>