页面载入中...

解决 WordPress 分页插件 Paginator 缺斜杠、双斜杠问题

Paginator 分页插件让用惯了 PageNavi 的 WordPress 用户耳目一新,但是安装后发现分页链接格式有点小问题,可能会缺少必要的斜杠或多出一个斜杠,导致 http://hellobmw.com/page/2 错误地变成 http://hellobmw.com/page2 或者 http://hellobmw.com/page//2。

据我推测,出现这种情况的 WordPress 可能设置了自定义 Permalink,并且 Permalink 格式最后没有斜杠(如:http://hellobmw.com/archives/xxx.html),另外问题主要集中在 category 和 tag 链接上(index 页没出问题是 WordPress 自动加上了"/")。

废话不说了,解决办法也很简单,那就是让插件视情况加减斜杠而不是一刀切,插件原本的代码忘了,只贴我改过的那一部分吧,大家仔细看两段代码细小的差别:

Thanks very much for Lilumi's great work to fix all the bugs of Paginator 3000.

非常感谢乌克兰的 Lilumi 对 Paginator 3000 斜杠 bug 的完美修正!我原来修正方法也是存在 bug 的,已经删掉了。下面是他的修改方法,在

function get_pagenum_link_without_queries()

中找到

$result = apply_filters('get_pagenum_link', $result);

在这行前面插入

if ($result[strlen($result)-1]==”/”) $result = substr($result,0,strlen($result)-1);

这样修改的目的是把所有链接进行统一,都把最后的斜杠去掉(如果有的话),然后再利用下面的代码统一只加一次斜杠:

echo(
    '<script type="text/javascript">
      if(navigator.appName=="Microsoft Internet Explorer"){
        var prevonload=window.onload;
        if(typeof window.onload!="function"){
          window.onload=function(){
            pag = new Paginator("paginator",'
. $wp_query->max_num_pages . ',' . $pages_to_show . ',' . $paged . ',"' . clean_url(get_pagenum_link_without_queries()).'/page/","'.get_pagenum_link_query().'");
          }
        }else{
          window.onload=function(){
            prevonload();
            pag = new Paginator("paginator",'
. $wp_query->max_num_pages . ',' . $pages_to_show . ',' . $paged . ',"' . clean_url(get_pagenum_link_without_queries()).'/page/","'.get_pagenum_link_query().'");
          }
        }
      }else{
        pag = new Paginator("paginator",'
. $wp_query->max_num_pages . ',' . $pages_to_show . ',' . $paged . ',"' . clean_url(get_pagenum_link_without_queries()).'/page/","'.get_pagenum_link_query().'");
      }
    </script>'
);

经过测试,这样就可以达到完美的效果啦!Now, Paginator is perfect!

24条留言. 发言 or Trackback

  • Young  21:59 Feb 23, 2009 

    真奇怪,怎么插件的作者没有来感谢你呢?

  • hhalloyy  02:01 Feb 24, 2009 

    :em09: 你真折腾。

  • 北极冰仔  09:40 Feb 24, 2009 

    @hhalloyy 从今天开始做正事,不想折腾了。 :em21:

  • Leeiio  15:15 Feb 25, 2009 

    受您感召,也用上這個插件了~我的Permalink後面有斜杠,所有沒有這個問題哈哈~

  • 北极冰仔  15:59 Feb 25, 2009 

    @Leeiio 好,这样便证明了我的推测是正确的。 :em17:

  • lilumi  06:04 Feb 27, 2009 

    thank you wery much for fast bug fixing

  • lilumi  06:14 Feb 27, 2009 

    but this bug now on other pages: http://hellobmw.com/tag/google/page/3
    do you now how to fix this?

  • 北极冰仔  11:04 Feb 27, 2009 

    @lilumi wow, it’s really frustrating that fixing a bug causes new bugs. I have no idea how to make it absolutely right at present but only some tricks…sorry.

  • lilumi  16:44 Feb 27, 2009 

    Woohoo — I’m fixed all bugs with slashes.
    in function get_pagenum_link_without_queries()

    before: $result = apply_filters('get_pagenum_link', $result);
    insert this: if ($result[strlen($result)-1]=="/") $result = substr($result,0,strlen($result)-1);

  • lilumi  16:45 Feb 27, 2009 

    Woohoo — I’m fixed all bugs with slashes.
    in function get_pagenum_link_without_queries()

    before: $result = apply_filters('get_pagenum_link', $result);
    insert this: if ($result[strlen($result)-1]=="/") $result = substr($result,0,strlen($result)-1);

    p.s. why your antispam system marking my comments as a spam???

  • 北极冰仔  17:33 Feb 27, 2009 

    @lilumi Yeah, you’re great! I have tested your method on my blog and it works perfectly! Thank you lilumi!

  • 北极冰仔  17:36 Feb 27, 2009 

    @lilumi Your comments were all approved without any problems. :)

  • lilumi  18:17 Feb 27, 2009 

    your antispam system don’t approve my comments when i fill the “site url” :(

  • 北极冰仔  20:29 Feb 27, 2009 

    @lilumi Is it true? Let me see what happened.

  • 北极冰仔  20:43 Feb 27, 2009 

    @lilumi
    I’m so sorry. My primary anti-spam plugin Spam Karma 2 put “org.ua” in its domain blacklist by mistake so that all comments whose urls end with “org.ua” were killed. Now I have put “org.ua” in the whiltelist, everything will be right. ;)

  • dzhus  03:01 Feb 28, 2009 

    Thanks for both of you guys for your contribution.
    I will address the issue in the next update.
    It will come out some time today or tomorrow.
    Sorry for inconvenience.

  • 北极冰仔  14:52 Feb 28, 2009 

    @dzhus You’re welcome. I really love your Paginator 3000. :-D

  • 小野大神  14:34 Mar 25, 2009 

    末尾斜杠WP貌似提供专门函数处理的- -。trailingslashit($str)在URL末尾加/ 。untrailingslashit去除/。还有user_trailingslashit根据用户设置的permalink末尾有没有/ 对URL处理。这几个函数都会判断URL末尾是否已有/,有的话不会重复添加。

  • 北极冰仔  18:37 Mar 25, 2009 

    @小野大神 谢谢!

  • 恒顺网.友  22:59 Apr 03, 2009 

    :em26: 大家好。

  • 恒顺网.友  23:03 Apr 03, 2009 

    :em01: 多谢大家的贡献。获益不浅。

    恒顺网.友
    http//zhjgxs.lingd.net

  • 疣迪  11:06 May 27, 2009 

    经典

  • rubberpixy  21:40 Jun 09, 2009 

    你好,站长,关于pagintar,似乎官方下载都是2.6.3的。我是2。7。1的我装后,没有反映,不知道如何解决。希望您能指点下,我php不是很懂。基本就是猜的~~

  • fd  13:49 Jun 10, 2009 

    菜鸟前来观光,感受一下熏陶 干洗 :em17:

留言 ↓

欢迎回来, (修改)

  • :em19:
  • :em01:
  • :em25:
  • :em09:
  • :em11:
  • :em00:
  • :em13:
  • :em08:
  • :em10:
  • :em03:
  • :em02:
  • :em05:
  • :em07:
  • :em16:
  • :em27:
  • :em14:
  • :em21:
  • :em20:
  • :em24:
  • :em18:
  • :em17:
  • :em06:
  • :em23:
  • :em12:
  • :em22:
  • :em15:
  • :em26:
  • :em04:

正在提交数据...