简体   繁体   English

在 Blogger 中按标签导航下一篇和上一篇文章

[英]Navigating Next and Prev Post By Label In Blogger

I wrote a code for navigating next and previous posts which are under the same label.我编写了一个代码,用于导航同一标签下的下一篇和上一篇文章。

All the posts in this blog will always be under 1 label only.此博客中的所有帖子将始终仅在 1 个标签下。 The blog is for a japanese comic (Manga) hosting, so the chapters will be under the 1 common label which is the title of the comic of the chapters.该博客是为日本漫画 (Manga) 托管的,因此章节将位于 1 common 标签下,该标签是章节漫画的标题。

The code is:代码是:

<style>
#button_block_container
{
    text-align:center;
}
#next, #prev
{
display: inline-block;
}
</style>
<script type="text/javascript">
 //<![CDATA[
 var pT="<data:post.title/>";
 function recentpostslist(json) {
 var eU=new Array();
 var eT=new Array();
 var current,k=0;
   for (var i = 0; i < json.feed.entry.length; i++)
   {
     for (var j = 0; j < json.feed.entry[i].link.length; j++) {
       if (json.feed.entry[i].link[j].rel == 'alternate') {
         break;
       }
     }
    eU[k] = "'" + json.feed.entry[i].link[j].href + "'";//bs
    eT[k] = json.feed.entry[i].title.$t;
    k++;
    }
    for(var i=0;i<k;i++)
    {
    if(eT[i]==pT)
      current=i;
    }
    nb(current,eT,eU,k);
 }
 function nb(c,eT,eU,k)
 {
  var np=c-1;
  var pp=c+1;
  if(c!=0)
  {
    if(np!=0)
    {
      var next="<a href="+eU[np]+"><img src='https://1.bp.blogspot.com/-vbAhPUydPwA/WWuen8w2rsI/AAAAAAAAVP4/rutiJBBKz2kYTclTUKtq6W2vBm8pi6uaACLcBGAs/s1600/if_go-next_118773.png' alt='Next Chapter' title='Next Chapter' height='48' width='48'/></a>";
     document.getElementById("next").innerHTML=next;
    }
    if(pp!=k)
    {
      var prev="<a href="+eU[pp]+"><img src='https://2.bp.blogspot.com/-jHxFGDn9aj0/WWueoDAidyI/AAAAAAAAVP8/O_okyJkKiE0j621B2b6d6AmWGDL_7SczACLcBGAs/s1600/if_go-previous_118774.png' alt='Previous Chapter' title='Previous Chapter' height='48' width='48'/></a>";
      document.getElementById("prev").innerHTML=prev;
    }
 }
}
 //]]> 
 </script>
 <b:loop values='data:post.labels' var='label'>
  <script expr:src='&quot;feeds/posts/summary/-/&quot;+data:label.name+&quot;?alt=json-in-script&amp;callback=recentpostslist&amp;max-results=999&quot;' type='text/javascript'/>
</b:loop>

The logic is perfect.逻辑是完美的。 It works when I put a title of some chapter in variable pT and I use this as the script call back当我将某个章节的标题放入变量pT并将其用作脚本回调时,它会起作用

<script src="http://www.example.com/feeds/posts/summary/-/**LABEL NAME**?max-results=150&alt=json-in-script&callback=recentpostslist"></script>

But when I put it in Blogger template I needed to use expr:src to make it accept data:label.name .但是当我把它放在 Blogger 模板中时,我需要使用expr:src让它接受data:label.name

By the way here is the explanation of the variables I used.顺便在这里解释一下我使用的变量。
eU is an array that has all the URL's of the posts under the given label stored in it. eU是一个数组,其中存储了给定标签下的所有帖子的 URL。
eT is an array that has all the Titles of the posts under the given label stored in it. eT是一个数组,其中存储了给定标签下的所有帖子标题。
pT is used for storing the name of the currently browsing chapter name. pT用于存放当前浏览的章节名称。
I am using for loop:我正在使用for循环:

for(var i=0;i<k;i++)
    {
    if(eT[i]==pT)
      current=i;
    }
    nb(current,eT,eU,k);

In here, it finds out the array index where the current post is located using comparing all the Post Title's in eT array with current if(eT[i]==pT) , stores the found index value in current and I send it to a function nb() with current index, and both arrays and last stored array index k .在这里,它通过将eT数组中的所有帖子标题与当前if(eT[i]==pT)进行比较来找出当前帖子所在的数组索引,将找到的索引值存储在current中,然后将其发送到具有当前索引的函数nb() ,以及数组和最后存储的数组索引k

np stores the index value of next post. np存储下一篇文章的索引值。 Since the posts are arranged in Recentpostlist order I had to do current-1 and Prev post pp as current+1 .由于帖子是按 Recentpostlist 顺序排列的,所以我必须将current-1和 Prev post pp设置为current+1

if(np!=0) for finding whether there are no more next posts and if(pp!=k) for finding whether there are no previous posts. if(np!=0)用于查找是否没有更多的下一篇文章,而if(pp!=k)用于查找是否没有以前的文章。

Now for the problem part: When I type the code in Blogger HTML template and save it and go to a post, there are no contents of post shown at all.现在是问题部分:当我在 Blogger HTML 模板中键入代码并将其保存并转到帖子时,根本没有显示帖子的内容。
Only footer and top header loads.仅加载页脚和顶部页眉。

And found out that the problem is with the callback script:并发现问题出在回调脚本上:

 <b:loop values='data:post.labels' var='label'>
 <script expr:src='&quot;feeds/posts/summary/-/&quot;+data:label.name+&quot;?alt=json-in-script&amp;callback=recentpostslist&amp;max-results=999&quot;' type='text/javascript'/>
</b:loop>

Please help.请帮忙。

The issue is happening because there is no space between the delimiters ( + and &quot; )发生此问题是因为分隔符( +&quot; )之间没有空格

The code should instead be -代码应该是 -

<b:loop values='data:post.labels' var='label'>
<script expr:src='&quot;feeds/posts/summary/-/&quot; +data:label.name+ &quot;?alt=json-in-script&amp;callback=recentpostslist&amp;max-results=999&quot;' type='text/javascript'/>
</b:loop>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM