如何使用自定义循环显示分页?

时间:2014-12-16 01:20:53

标签: php wordpress posts

我一整天都在不停地搜索StackOverflow,寻找解决问题的方法。出于某种原因,我无法通过此循环正确地进行分页。起初我使用" query_posts",虽然每个页面都有相同的帖子。这是我的代码......

<?php
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                $args = array(
                    'cat' => '319',
                    'posts_per_page' => 8,
                    'paged' => $paged
                );
                $loop = new WP_Query( $args );

                if ( $loop->have_posts() ) {
                    /* The Loop */
                    while ( $loop->have_posts() ) {
                        $loop->the_post();
                        // do something
                        echo '<li>';
                        echo '<a class="syncopate" href="';
                        the_permalink();
                        echo '">';
                        the_title();
                        echo '</a>';
                        echo '</li>';
                    }
                } ?>
                <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
                <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
                <?php wp_reset_query(); ?>

看起来这应该有效,但为什么不呢?

这是在pagination.php ...

中调用的函数
function html5wp_pagination()
 {
  global $wp_query;
  $big = 999999999;
  echo paginate_links(array(
    'base' => str_replace($big, '%#%', get_pagenum_link($big)),
    'format' => '?paged=%#%',
    'current' => max(1, get_query_var('paged')),
    'total' => $wp_query->max_num_pages
  ));
  }

1 个答案:

答案 0 :(得分:0)

所以,这最终为我工作。任何人都可以向我解释为什么这有效,而我的原始代码没有?

$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
                $temp = $wp_query;
                $wp_query = null;
                $wp_query = new WP_Query();
                $wp_query -> query('cat=319&posts_per_page=10'.'&paged='.$paged);
                while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
                    <div class="post">
                        <?php echo '<li>';
                            echo '<a class="syncopate" href="';
                        the_permalink();
                        echo '">';
                                the_title();
                                echo '</a>';
                            echo '</li>'; ?>
                    </div>
                <?php endwhile; // end of the loop. ?>

                <div class="pagenav">
                    <div class="alignleft"><?php previous_posts_link('Previous') ?></div>
                    <div class="alignright"><?php next_posts_link('Next') ?></div>
                </div>

                <?php $wp_query = null; $wp_query = $temp; ?>