如何更改此所有帖子列表代码以列出特定标签?

问题描述 投票:0回答:1

我正在使用此脚本来显示我网站上的所有帖子。但是现在我想更改此代码以显示特定的带标签的帖子。但是我不知道该怎么做。我已经尽力了。我是编码的新手。任何帮助都将大大赞赏

var startIndex = 1;
var maxResults = 150;
var allResults = [];
function sendQuery12()
{
   var scpt = document.createElement("script");
   scpt.src = "http://yoursite.com/feeds/posts/summary?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
  
         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");
                  a1E.href = url;
                  a1E.textContent = title + " (" + date.substr(0,10) + ")";
                  liE.appendChild(a1E);
                  //elmt.appendChild(liE);
                  allResults.push(liE);
               }
               break;
            }
         }
      }
      if (feed.entry.length >= maxResults)
      {
         startIndex += maxResults;
         sendQuery12();
      } else {
         printArrayResults();
      }
   }
}
sendQuery12();
<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: center;">
<span style="color: blue;">&nbsp;<span style="font-size: x-small;">(All post)</span></span></div>
<div style="background-color: #fffad6; border: 0px solid BLACK;">
<center>
<script>function mbhTotalCount(json) {document.write(parseInt(json.feed.openSearch$totalResults.$t,10));}</script><div style="font: 20px Domestic Manners;">
<span style="color: green;">Total:<b><span style="color: red;"> <script src="http://yoursite.com/feeds/posts/default?alt=json-in-script&amp;callback=mbhTotalCount"></script></span></b></span></div>
</center>
</div>
<hr />
<div>
<ul id="postList12"></ul>
</div>
</div>
javascript html web blogger blogs
1个回答
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

     // This is an example, change to appropriate property
     var label = entry.label.$t

     for (var j = 0; j < entry.link.length; j++)
     {
        // desiredLabels = ['foo', 'bar', 'baz']
        if (!desiredLabels.includes(label) {
           continue;
        }
        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");
              a1E.href = url;
              a1E.textContent = title + " (" + date.substr(0,10) + ")";
              liE.appendChild(a1E);
              //elmt.appendChild(liE);
              allResults.push(liE);
           }
           break;
        }
     }
  }
© www.soinside.com 2019 - 2024. All rights reserved.