WordPress 分页在主页上不起作用(自定义模板) [英] WordPress pagination not working on home page ( Custom templete )

查看:59
本文介绍了WordPress 分页在主页上不起作用(自定义模板)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用谷歌搜索了这个问题,我担心不仅是我还有很多程序员都面临这个问题.我在那里申请了修复,但结果相同.你能检查一下我的分页码吗?我有一个静态首页,分页将我重定向到主页 (/).如果我在 url /page/2 之后手动添加这个也会将我重定向到主页.我知道这是幼稚的问题,但我无法弄清楚问题所在.

I have googled the problem, I am worried not only me there are many coder who faces this problem. I applied there fixing but same result. Would you please review my pagination code? I have a static front page, where pagination redirects me to homepage (/). If I add after url /page/2 manually this one also redirect me to homepage. I know this is childish question but I can't figure out the problem.

这是函数.

 function pagination($pages = '', $range = 2)
 {  
     $showitems = ($range * 2)+1;  

     global $paged;
     if(empty($paged)) $paged = 1;

     if($pages == '')
     {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }   

     if(1 != $pages)
     {
         echo "<ul class='pagenavi'>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<li><a href='".get_pagenum_link(1)."'>&laquo;</a></li>";
         if($paged > 1 && $showitems < $pages) echo "<li><a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a></li>";

         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<li><a href='".get_pagenum_link($i)."' class='current' >".$i."</a></li>":"<li><a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a></li>";
             }
         }

         if ($paged < $pages && $showitems < $pages) echo "<li><a href='".get_pagenum_link($paged + 1)."'>&rsaquo;</a></li>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<li><a href='".get_pagenum_link($pages)."'>&raquo;</a></li>";
         echo "</ul>\n";
     }
}

用于生成分页的代码

<?php query_posts( array( 'post_type' => 'post', 'paged' => $paged, 'cat'=> $bcatid, 'posts_per_page' => 9 ) );
?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
post content
<?php endwhile; endif; ?>
<?php pagination(); ?>

预先感谢您抽出宝贵时间.

Thanks in advance for your valuable time.

推荐答案

在静态首页(页面模板)上获取当前分页号的正确方法你必须使用'page'查询变量:

The correct way to get the current pagination number on a static front page (Page template) you have to use the 'page' query variable:

$paged = (get_query_var('page')) ?get_query_var('page') : 1;

更多信息在这里:http://codex.wordpress.org/Function_Reference/get_query_var

这篇关于WordPress 分页在主页上不起作用(自定义模板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆