0

我有一个有分页的类别循环。第一页永远是对的。一旦我通过第一页,它就会将类别混合在一起。此外,页数总是错误的。

<?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)
    ));
}
?>
4

1 回答 1

0

弄清楚了:

paginate_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,
    ));
}
?>
于 2013-02-25T19:54:20.890 回答