【问题标题】:Infinite Scroll pathParse for reverse WordPress CommentsInfinite Scroll pathParse 用于反向 WordPress 评论
【发布时间】:2012-07-09 14:56:34
【问题描述】:

我正在使用 jQuery Infinite Scroll 插件 - https://github.com/paulirish/infinite-scroll/ 显示我的分页 WordPress cmets。

插件在从旧到新查看 cmets 时工作正常(我认为这是默认选项),但如果 WordPress 中的讨论选项设置为以下:

Break comments into pages with [XX] top level comments per page and the
[LAST] page displayed by default

Comments should be displayed with the [NEWER] comments at the top of each page

然后无限滚动不再起作用。

查看问题,似乎是因为如果设置如上,那么WordPress将显示的第一个cmets页面是最后一个,即

WordPress 1st comment page displayed = http://MYLINK/comment-page-5
WordPress 2nd comment page displayed = http://MYLINK/comment-page-4
WordPress 3rd comment page displayed = http://MYLINK/comment-page-3

等等

但是,我认为无限滚动想要增加每一页,所以在显示第一页之后(实际上是第5页)无限滚动然后寻找第6页,它不存在。

查看 IS 选项,有一个 pathParse 选项 - 但没有说明如何使用它的文档。我什至不能 100% 确定这是否会有所帮助。

我(和许多其他人)会非常感谢您提供的任何帮助。

【问题讨论】:

    标签: jquery wordpress infinite-scroll


    【解决方案1】:

    Thought Id 在这里提供了一个更简单的解决方案。 我没有尝试更改插件,而是发现反转 cmets 数组更容易(经过几天和几天的尝试)。 只需将其添加到您的functions.php(来自here

    if (!function_exists('iweb_reverse_comments')) {
    function iweb_reverse_comments($comments) {
        return array_reverse($comments);
        }   
    }
    add_filter ('comments_array', 'iweb_reverse_comments');
    

    这样,infinitescroll js 可以保持原样。此外,您只需将 Wordpress 设置 > 讨论保留为默认设置。

    【讨论】:

      【解决方案2】:

      插件从div.navigation a:first 选择器获取下一个要加载的 URL。它的 href 属性作为路径传递给下一页的 ajax 请求。在控制台中尝试那个 jQuery 选择器,看看它会产生什么;然后,您可以更改插件以重写选择器,或者更改您的 HTML 以便选择器获得正确的匹配。

      尝试 2

      解析问题不是因为编号;它正在寻找page=3,您的链接是page-3。假设无法更改,但您可以按照建议添加 pathParse 方法(在代码中注释掉类似方法的地方添加此方法):

       ,pathParse:function(path,nextPage){
         path = path.match(/page[-=]([0-9]*)/).slice(1);
         return path;
       }
      

      然后让它正确地减少而不是增加我目前能看到的唯一方法是将第 493 行(在 WP 插件的开发版本中)更改为读取

      opts.state.currPage--;
      

      【讨论】:

      • 你好。我已经尝试过 a:first 选择器,但它并没有改变任何东西。我的控制台中仍然出现错误:[“抱歉,我们无法解析您的下一篇(上一篇文章)URL。验证您的 css 选择器指向正确的 A 标签。如果您仍然收到此错误:大喊、尖叫和请在infinite-scroll.com寻求帮助。"] plugins.js:41 ["determinePath", "URL/comment-page-3#cmets"] plugins.js:41 ["Binding", "bind"]'这与 IS 增加它尝试加载的页面有关,而我的页面 URL 数量减少...
      • IS 不应该增加它们;它会寻找“下一个”URL 来查找要加载的内容,因此不应该做任何其他事情(如果它增加了它会加载 next+1)。有趣的是,如果您点击 cmets url 的链接,无限滚动效果很好。我会更好看的。
      • 在代码中评论// find the number to increment in the path.我的错!那我们就解决这个问题吧!
      【解决方案3】:

      我似乎想出了一个(不完全优雅的)解决这个问题的方法。

      非常感谢@M1ke 的帮助。

      好的,

      首先,您需要使用 pathParse 函数,以便在哪里定义无限滚动选项:

      添加

      .infinitescroll({
          state: {
            currPage: 4 // The number of the first comment page loaded, you can generate this number dynamically to save changing it manually
          },        
      
          pathParse: function(path,nextPage){
              path = ['comment-page-','#comments'];
              return path;
          }
      });
      

      然后您需要调整主插件文件(jquery.infinitescroll.js 或 .min 版本),因为似乎没有其他方法可以做到这一点。因此,找到包含以下内容的部分:

      // increment the URL bit. e.g. /page/3/
      opts.state.currPage++;
      

      然后改成

      // decrement the URL bit. e.g. /page/3/
      if (opts.state.currPage > 1) {
          opts.state.currPage--;
      }
      else if (opts.state.currPage == 1) {
          console.log("Last Page"); // Just needed for debugging              
          opts.state.currPage = 999999; // stop IS from loading Comment Page 0 by giving it a page number that won't exist and will return a '404' to provide 'end' function.
      }
      

      另外,请确保以下部分:

      this._debug('pathParse manual');
      return opts.pathParse(path, this.options.state.currPage+1);
      

      改为:

      this._debug('pathParse manual');
      return opts.pathParse(path, this.options.state.currPage);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多