如何更改此代码以列出Blogger上所有帖子的帖子标题,格式为YYYY.MM.DD <title>和时间顺序?

时间:2017-05-18 04:06:10

标签: javascript list blogger

&lt; p&gt;我想在特定页面上显示我的帖子标题。了解作者所写内容比滚动所有页面或使用归档小部件导航更有效。&lt; / p&gt; &lt; p&gt;我发现生成列表的代码(代码如下)按字母顺序对帖子标题进行排序,但我喜欢按时间顺序显示标题。关于这个有很多代码示例,但它们已经过时了。在博客平台发生一些变化后,它们不再起作用了。&lt; / p&gt; &lt; p&gt;如何更改代码以按时间顺序和格式YYYY.MM.DD获取帖子标题?&lt; / p&gt; &LT;预&GT;&LT;代码&GT;&LT; DIV&GT;     &lt; ul id =&#34; postList12&#34;&gt;&lt; / ul&gt; &LT; / DIV&GT; &lt; script type =&#34; text / javascript&#34;&gt;     var startIndex = 1;     var maxResults = 150;     var allResults = [];     function sendQuery12(){         var scpt = document.createElement(&#34; script&#34;);         scpt.src =&#34; / feeds / posts / summary?alt = json&amp; callback = processPostList12&amp; start-index =&#34; + startIndex +&#34;&amp; max-results =&#34; + maxResults;         document.body.appendChild(SCPT);     }     function printArrayResults(root){         // Alphebetically排序         allResults.sort(函数(a,b)         {             var a_string = a.children [0] .textContent;             var b_string = b.children [0] .textContent;             if(a_string&lt; b_string)返回-1;             if(a_string&gt; b_string)返回1;             返回0;         })         var elmt = document.getElementById(&#34; postList12&#34;);         for(index = 0; index&lt; allResults.length; index ++){             elmt.appendChild(allResults [指数]);         }     }     function processPostList12(root){         var elmt = document.getElementById(&#34; postList12&#34;);         if(!elmt)             返回;         var feed = root.feed;         if(feed.entry.length&gt; 0){             for(var i = 0; i&lt; feed.entry.length; i ++){                 var entry = feed.entry [i];                 var title = entry.title。$ t;                 var date = entry.published。$ t                 for(var j = 0; j&lt; entry.link.length; j ++){                     if(entry.link [j] .rel ==&#34; alternate&#34;){                         var url = entry.link [j] .href;                         if(url&amp;&amp; url.length&gt; 0&amp;&amp; title&amp;&amp; title.length&gt; 0){                             var liE = document.createElement(&#34; li&#34;);                             var a1E = document.createElement(&#34; a&#34;);                             a1E.href = url;                             a1E.textContent = title +&#34; (&#34; + date.substr(0,10)+&#34;)&#34 ;;                             liE.appendChild(A1E);                             //elmt.appendChild(liE);                             allResults.push(IIc)的;                         }                         打破;                     }                 }             }             if(feed.entry.length&gt; = maxResults){                 startIndex + = maxResults;                 sendQuery12();             } else {                 printArrayResults();             }         }     }     sendQuery12(); &LT; /脚本&GT; &LT; /代码&GT;&LT; /预&GT; &lt; p&gt;代码从此处复制:&lt; a href =&#34; https://dansator.blogspot.fi/2015/10/general-alphabetical-list-of-posts.html" rel =&#34; nofollow noreferrer&#34;&gt; https://dansator.blogspot.fi/2015/10/general-alphabetical-list-of-posts.html< / a&gt;&lt; / p&gt;

1 个答案:

答案 0 :(得分:0)

从代码中删除sort方法。删除以下内容:

//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;
   })

您的代码应为

<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?alt=json&callback=processPostList12&start-index=" + startIndex + "&max-results=" + maxResults;
   document.body.appendChild(scpt);
}

function printArrayResults(root)
{  
   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();
</script>