为什么第2页会出现字分页制动?

时间:2013-02-25 19:42:44

标签: wordpress loops pagination categories

我有一个具有分页的类别循环。第一页总是正确的。一旦我越过第一页,它就会将类别混合在一起。此外,页数总是错误的。

<?php
    global $paged;
    $cat = get_the_category();
    $catSlug = $cat[1]->slug;
    $query = new WP_Query(array('category_name' => $catSlug,'posts_per_page' => 20,'paged' => $paged));
 ?>
<?php
    while ( $query->have_posts() ) :
        $query->the_post();
?>
        <li><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( '%s', 'starkers' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; ?>
    </ul>
<?php
    global $wp_query;

    $catSlug = $cat[0]->slug;
    $total_pages = $wp_query->max_num_pages;

    if ($total_pages > 1){

  $current_page = max(1, get_query_var('paged'));

  echo paginate_links(array(
      'base' => get_pagenum_link(1) . '%_%',
      'format' => '?paged=%#%',
      'current' => $current_page,
      'total' => $total_pages,
      'add_args' => array('category_name' => $catSlug)
    ));
}
?>

1 个答案:

答案 0 :(得分:0)

想出来:

pginate_links()需要

query_posts()才能工作。我不得不改变query_posts而不是使用wp_query,如下所示:

<?php
    global $query_string;
    query_posts( $query_string . '&posts_per_page=20' );
?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
        <li><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( '%s', 'starkers' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; ?>
    </ul>
<?php
    global $wp_query;

$total_pages = $wp_query->max_num_pages;

if ($total_pages > 1){

  $current_page = max(1, get_query_var('paged'));

  echo paginate_links(array(
      'base' => get_pagenum_link(1) . '%_%',
      'format' => '?paged=%#%',
      'current' => $current_page,
      'total' => $total_pages,
    ));
}
?>