分页第2页在wordpress中不起作用

时间:2017-06-14 18:03:45

标签: wordpress pagination

我的页面上存在分页问题。 我使用插件WP-Paginate。它在index.php上工作正常,但在category.php中不起作用。当我想转到下一页时,它将我重定向到index.php,没有帖子???

这是我的category.php代码:

    <?php     
         $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
            $args = array(
               'posts_per_page' => 10,
               'paged' => $paged
            );
    $cat_query = new WP_Query( $args ); ?>

    <?php if ($cat_query->have_posts()) : ?>
    <?php while ($cat_query->have_posts()) : $cat_query->the_post(); ?>

           POST TEMPLATE

    <?php endwhile; ?>
    <?php else : ?>

// 

    <?php endif; ?>
    <?php wp_paginate(); ?>

希望我们能修复它

问候!

1 个答案:

答案 0 :(得分:0)

您可以使用以下功能创建自定义分页。将此代码放在functions.php文件中

function pagination($pages = '', $range = 1){
     $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 "<div class=\"pagination\"><span class='page-number'>Page ".$paged." of ".$pages."</span>";
        if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a class='first' href='".get_pagenum_link(1)."'>&laquo; First </a>";
        if($paged > 1 && $showitems < $pages) echo "<a class='preview' href='".get_pagenum_link($paged - 1)."'>&lsaquo; </a>";

        for ($i=1; $i <= $pages; $i++){
            if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
                echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
            }
        }
        if ($paged < $pages && $showitems < $pages) echo "<a class='next' href=\"".get_pagenum_link($paged + 1)."\">&rsaquo;</a>";  
        if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a class='last' href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
        echo "</div>\n";
    }
}

输入此代码后,只需在要显示分页的位置调用该函数。

if (function_exists("pagination")) {
     pagination($query->max_num_pages); //replace $query to $cat_query
}