获取上一篇文章和下一篇文章的标题

get previous and next post titles

提问人:practice 提问时间:11/25/2022 更新时间:11/25/2022 访问量:86

问:

我正在努力从默认导航开始向 Blogger 添加下一个和上一个导航按钮,并向其添加帖子标题

我尝试使用 Blogger 提要获取帖子的标题,但由于某种原因,它说没有定义回调函数

这是代码

 <a expr:href='data:olderPageUrl'>
                      <div class='older-post'>
                        <span class='pager_icon'><svg class='line' viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'><polyline points='15 18 9 12 15 6'/></svg></span>
                        <span>prev</span>
                        <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'><span class='older-text'><data:olderPageTitle/></span></a>
                      </div>
                    </a>
                    <a expr:href='data:newerPageUrl'>
                      <div class='newer-post'>
                        <span class='pager_icon'><svg class='line' viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'><polyline points='9 18 15 12 9 6'/></svg></span>
                        <span>next</span>
                        <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'><span class='newer-text'><data:newerPageTitle/></span></a>
                      </div>
                    </a>
 /*********** Between Posts Navigation Starts ************/
  var olderLink = document.getElementById('Blog1_blog-pager-older-link');
  if (olderLink) {
    getPageTitle(olderLink, setOlderPageTitle);
    function setOlderPageTitle(data){
      setPageTitle(data, olderLink, '', '')
    };
  }
  var newerLink = document.getElementById('Blog1_blog-pager-newer-link');
  if (newerLink) {
    getPageTitle(newerLink, setNewerPageTitle);
    function setNewerPageTitle(data){
      setPageTitle(data, newerLink, '', '')
    };
  }
  // set the page title from feed data
  function setPageTitle(data, pageLink, prefix, suffix) {
    if (data.feed.entry) {
      if (data.feed.entry.length > 0) {
        var title = data.feed.entry[0].title.$t;
      }
    }
    if (title) {
      pageLink.innerHTML = prefix + title + suffix;
    }
  }
  // get entry data from the feed
  function getPageTitle(pageLink, callback) {
      var pathname = pageLink.getAttribute('href').replace(location.protocol + '//' + location.hostname, '');
      var script = document.createElement('script');
      script.src = '/feeds/posts/summary?alt=json-in-script&max-results=1&redirect=false&path='+pathname+'&callback='+callback.name+'';
   var headTg =  document.getElementsByTagName('head')[0];
      document.body.appendChild(script);
  }
 /*********** Between Posts Navigation End ************/
JavaScript jQuery 正则表达式 回调 博客

评论

0赞 Twisty 11/25/2022
欢迎使用 Stack Overflow。我看到您在哪里定义了许多函数,但可能存在操作顺序问题,因为您在调用函数之后定义了这些函数,

答:

0赞 Twisty 11/25/2022 #1

请考虑以下几点。

$(function() {
  /*********** Between Posts Navigation Starts ************/
  var olderLink = $('#Blog1_blog-pager-older-link');
  var newerLink = $('#Blog1_blog-pager-newer-link');
  // set the page title from feed data
  function setPageTitle(data, pageLink, prefix, suffix) {
    if (data.feed.entry) {
      if (data.feed.entry.length > 0) {
        var title = data.feed.entry[0].title.$t;
      }
    }
    if (title) {
      pageLink.innerHTML = prefix + title + suffix;
    }
  }

  // get entry data from the feed
  function getPageTitle(pageLink, callback) {
    var pathname = pageLink.attr('href').replace(location.protocol + '//' + location.hostname, '');
    var script = $('<script>', {
      src: '/feeds/posts/summary?alt=json-in-script&max-results=1&redirect=false&path=' + pathname + '&callback=' + callback.name
    }).appendTo("body");
  }

  function setOlderPageTitle(data) {
    setPageTitle(data, olderLink, '', '')
  };

  function setNewerPageTitle(data) {
    setPageTitle(data, newerLink, '', '')
  };

  if (olderLink) {
    getPageTitle(olderLink, setOlderPageTitle);
  }
  if (newerLink) {
    getPageTitle(newerLink, setNewerPageTitle);
  }
  /*********** Between Posts Navigation End ************/

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a expr:href='data:olderPageUrl'>
  <div class='older-post'>
    <span class='pager_icon'><svg class='line' viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'><polyline points='15 18 9 12 15 6'/></svg></span>
    <span>prev</span>
    <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'><span class='older-text'><data:olderPageTitle/></span></a>
  </div>
</a>

<a expr:href='data:newerPageUrl'>
  <div class='newer-post'>
    <span class='pager_icon'><svg class='line' viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'><polyline points='9 18 15 12 9 6'/></svg></span>
    <span>next</span>
    <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'><span class='newer-text'><data:newerPageTitle/></span></a>
  </div>
</a>

评论

0赞 practice 11/25/2022
我设法解决了这个问题,该问题只是在加载 DOM 时执行该功能,而不是在加载 DOM 之后,除了上面的功能工作正常之外,谢谢