简体   繁体   English

在 Blogger 中浏览相同标签下的下一个和上一个帖子

[英]Navigating Next And Prev Posts Under Same Labels in Blogger

For starters I want you to read首先,我希望你阅读
Next and Prev Buttons for Blogger Theme Blogger 主题的下一个和上一个按钮

Understood the concept of what I want?明白我想要什么的概念了吗? Then let's continue with my question :然后让我们继续我的问题:

Now I have the code that was already embedded to the theme for next and prev buttons and they were :现在我有已经嵌入到主题中的下一个和上一个按钮的代码,它们是:

<nav class='op-pagi'>
<a class='blog-pager-older-link' expr:href='data:olderPageUrl' expr:id='data:widget.instanceId + &quot;_blog-pager-older-link&quot;' expr:title='data:olderPageTitle'><i class='fa fa-angle-left'/> Prev</a>
<a class='blog-pager-newer-link' expr:href='data:newerPageUrl' expr:id='data:widget.instanceId + &quot;_blog-pager-newer-link&quot;' expr:title='data:newerPageTitle'> Next <i class='fa fa-angle-right'/></a>
</nav>

And Using the questions I had asked before like :并使用我之前问过的问题:
How to store the URL's and Titles of a list of posts under same label into a String array in Blogger AND 如何将同一标签下的帖子列表的 URL 和标题存储到 Blogger 中的字符串数组中
How to match and point to a particular data in array in Blogger 如何在 Blogger 中匹配并指向数组中的特定数据

I made a code myself :我自己做了一个代码:

<script>
//<![CDATA[

<b:if cond='data:blog.searchLabel'>

  var URLArray = <b:eval expr='data:posts map (post =&gt; post.url)'/>;
  var TitleArray = <b:eval expr='data:posts map (post =&gt; post.title)'/>;  

var cURL = "<data:post.url/>";
var gTitle = "<data:post.title>";
function IndexFinder(element,index) {
   return element == cURL
}
function IndexFinderT(element,index){
   return element == gTitle;
}
var sU = URLArray.findIndex(IndexFinder);
var sT = TitleArray.findIndex(IndexFinderT);

function prevURL(){
    var prevU=URLArray[sU-1];
return prevU;
}
function prevTitle(){
    var prevT=TitleArray[sT-1];
return prevT;
}
function nextURL(){
    var nextU=URLArray[sU+1];
return nextU;
}
function nextTitle(){
    var nextT=TitleArray[sT+1];
return nextT;
}

</b:if>
//]]>
</script>

Now I tried Using the code which I showed first for next and prev button by changing expr:href='data:olderPageUrl' to expr:href='prevURL()'现在我尝试使用我首先为 next 和 prev 按钮显示的代码,将expr:href='data:olderPageUrl'更改为expr:href='prevURL()'
and the same for Title also标题也一样
expr:title='data:olderPageTitle' to expr:title='prevTitle()' expr:title='data:olderPageTitle'expr:title='prevTitle()'
but didn't work.但没有用。

Can you please tell me where I went wrong and how to make it work?你能告诉我我哪里出错了以及如何使它起作用吗?

Note: I'm using CDATA Tag for no particular reason, just put it there thinking it might help in someway.注意:我没有特殊原因使用CDATA标签,只是把它放在那里,认为它可能在某种程度上有所帮助。 I didn't understand its exact usage even when I searched on google.即使我在谷歌上搜索,我也不明白它的确切用法。 If it's causing any problem please mention it.如果它导致任何问题,请提及它。

Using CDATA prevents blogger XML parser from interpreting data tags like <b:if cond='data:blog.searchLabel'>使用 CDATA 可以防止博客 XML 解析器解释像<b:if cond='data:blog.searchLabel'>这样的数据标签

Remove //<![CDATA[ and //]]> from the code or use them after blogger tags :从代码中删除//<![CDATA[//]]>或在博客标签后使用它们:

<script>

<b:if cond='data:blog.searchLabel'>

var URLArray = <b:eval expr='data:posts map (post =&gt; post.url)'/>;
var TitleArray = <b:eval expr='data:posts map (post =&gt; post.title)'/>;  
var cURL = "<data:post.url/>";
var gTitle = "<data:post.title>";

//<![CDATA[

function IndexFinder(element,index) {
   return element == cURL
}
function IndexFinderT(element,index){
   return element == gTitle;
}
var sU = URLArray.findIndex(IndexFinder);
var sT = TitleArray.findIndex(IndexFinderT);

function prevURL(){
    var prevU=URLArray[sU-1];
return prevU;
}
function prevTitle(){
    var prevT=TitleArray[sT-1];
return prevT;
}
function nextURL(){
    var nextU=URLArray[sU+1];
return nextU;
}
function nextTitle(){
    var nextT=TitleArray[sT+1];
return nextT;
}

//]]>

</b:if>

</script>

As both prevURL() and prevTitle() are JavaScript functions and not Blogger data tags, you don't need to use the expr in front of the attribute.由于prevURL()prevTitle()都是 JavaScript 函数而不是 Blogger 数据标签,因此您无需在属性前使用expr

You will instead need to modify the functions as follows -您将需要按如下方式修改功能 -

function prevURL(){
    var prevU=URLArray[sU-1];
    document.querySelector('.blog-pager-older-link').href = prevU;
}

function prevTitle(){
    var prevT=TitleArray[sT-1];
    document.querySelector('.blog-pager-older-link').title = prevT;
}

and similarly for the next page links.下一页链接也类似。 No need to change the HTML of the page.无需更改页面的 HTML。

Also as Bassam mentioned in the other answer.也正如Bassam在另一个答案中提到的那样。 Don't wrap the JavaScript in CDATA directive otherwise, the XML parser that Blogger uses will completely ignore the script block and no data tag will get evaluated不要将 JavaScript 包裹在CDATA指令中,否则 Blogger 使用的 XML 解析器将完全忽略脚本块并且不会评估任何数据标签

You can paste this code in the posting widget您可以将此代码粘贴到发布小部件中

<b:includable id='pager_chapter' var='type'>
<b:if expr:cond='data:post.labels any (l =&gt; l.name != data:type.name)'>
  <script type='text/javascript'>var selectchap = false;</script>
</b:if>
<b:if expr:cond='data:post.labels any (l =&gt; l.name == data:type.name)'>
  <script type='text/javascript'>var selectchap = true;</script>
</b:if>
<b:loop index='i' values='data:post.labels' var='label'>
  <b:if cond='data:i == 0'>
  <div class='pager-js rounded' id='pager-js'>
  <div class='_prev' id='prevjs'><a class='pager-js-nolink'><span class='material-icons' style='font-size:2.5rem;'>chevron_left</span></a></div>
  <div class='_index' id='indexjs'><a class='pager-js-nolink'><span class='material-icons' style='font-size:2.5rem;'>list</span></a></div>
  <div class='_next' id='nextjs'><a class='pager-js-nolink'><span class='material-icons' style='font-size:2.5rem;'>chevron_right</span></a></div>
  </div>
  <script type='text/javascript'>
    var postPrev = &quot;<span class='material-icons' style='font-size:2.5rem;'>chevron_left</span>&quot;;
    var postNext = &quot;<span class='material-icons' style='font-size:2.5rem;'>chevron_right</span>&quot;;
    var postIndex = &quot;<span class='material-icons' style='font-size:2.5rem;'>list</span>&quot;;
    var postID = <data:post.id/>;
    /*<![CDATA[*/
    if(selectchap==false){
      var yktocs=function(data){
        var i=0;
        for(;
        i<data.feed.entry.length;
        i++){
          var j=0;
          for(;
          j<data.feed.entry[i].link.length;
          j++){
            if("alternate"==data.feed.entry[i].link[j].rel){
              var entityUrl=data.feed.entry[i].link[j].href;
              break
            }
          }
          document.getElementById("indexjs").innerHTML="<a href='"+entityUrl+"' title='Index Novel'>"+postIndex+"</a>"
        }
      };
      var ykpager=function(data){
        var node={
          postlist:[]
        };
        var i=0;
        for(;
        i<data.feed.entry.length;
        i++){
          var j=0;
          for(;
          j<data.feed.entry[i].link.length;
          j++){
            if("alternate"==data.feed.entry[i].link[j].rel){
              var murl=data.feed.entry[i].link[j].href;
              break
            }
          }
          var videoId=data.feed.entry[i].id.$t;
          var CAPTURE_ID=videoId.substr(51,19);node.postlist.push({
            url:murl,id:CAPTURE_ID
          })
        }var v=node.postlist.findIndex((timeline_mode)=>{
          return timeline_mode.id==postID
        });
        var id=v+1;
        var cbs=node.postlist[id];
        if(null!=cbs){var g=Object.values(cbs)[0];
          var lnkDiv=document.getElementById("prevjs");
          lnkDiv.innerHTML="<a href='"+g+"' title='Chapter Sebelumnya'>"+postPrev+"</a>"
        }
        var q=v-1;
        var val=node.postlist[q];
        if(null!=val){
          var k=Object.values(val)[0];
          lnkDiv=document.getElementById("nextjs");
          lnkDiv.innerHTML="<a href='"+k+"' title='Chapter Selanjutnya'>"+postNext+"</a>"
        }
      }
    }
    else{document.write("<style>.pager-js{display:none;}</style>")
  };
    /*]]>*/
  </script>
  <script defer='defer' expr:src='&quot;/feeds/posts/summary/-/&quot; + data:label.name + &quot;?alt=json-in-script&amp;callback=ykpager&amp;max-results=99999999&quot;' type='text/javascript'/>
  <script defer='defer' expr:src='&quot;/feeds/posts/summary/-/&quot; + data:type.name + &quot;/&quot; + data:label.name + &quot;?orderby=published&amp;alt=json-in-script&amp;callback=yktocs&amp;max-results=1&quot;' type='text/javascript'/>
</b:if>
</b:loop>
</b:includable>

And paste code below After <data:post.body/>并在<data:post.body/>之后粘贴下面的代码

<b:include data='{name : &quot;Novel&quot;}' name='pager_chapter'/>

You can read full tutorial How To Make Next and Prev Button By Label on Blogger您可以阅读完整教程如何在 Blogger 上按标签制作下一个和上一个按钮

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

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