wp_query / wordpress中的分页

时间:2015-03-30 10:47:50

标签: wordpress

我已经在Wordpress中为自定义循环编写了这段代码,但我似乎无法分页。

<?php //Template Name: Acapellas ?>
<?php get_header(); ?>
<?php if (is_user_logged_in()) { ?>
<div id="main-content">
    <div class="container">
        <div class="row">
            <div class="col-md-3">
                <?php get_sidebar('primary-left'); ?>
            </div>
            <div class="col-md-9">
                <div class="post-<?php the_ID(); ?> post-content">
                    <h1>Acapellas</h1>
                    <?php $loop = new WP_Query( array( 'post_type' => 'acapella', 'posts_per_page' => 100 ) ); ?>
                    <ul class="acapellas row">
                    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
                    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
                        <li class="post-<?php the_ID(); ?> col-md-6">
                            <div class="wrap">
                                <h2><?php the_title() ?></h2>

                                <?php the_content(); ?>

                                <span class="download left"><?php get_attachment_icons($echo=true); ?></span>

                                <span class="list-date right">First added: <?php the_time('F jS, Y') ?></span><br>

                               <?php
                                global $post;
                                $post_type            = get_post_type(get_the_ID());
                                $post_type_taxonomies = get_object_taxonomies($post_type);

                                if (!empty($post_type_taxonomies)) {

                                    echo '<ul class="details">';

                                    foreach ($post_type_taxonomies as $taxonomy) {

                                        $terms = get_the_term_list(get_the_ID(), $taxonomy, '', '</li><li>', '');


                                        if ($terms) {
                                            echo '<li>' . $terms . '</li>';
                                        }
                                    }

                                    echo '</ul>';
                                }
                                ?>
                            </div>
                        </li>
                    <?php endwhile; ?>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>
<?php
} else {
  wp_redirect( 'http://www.voclr.it/signup' ); exit;
}
?>
<?php get_footer(); ?>

1 个答案:

答案 0 :(得分:0)

将$ paged变量移出循环并将其作为WP_Query

的参数包含在内
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query( array( 
    'post_type'      => 'acapella', 
    'posts_per_page' => 100,
    'paged'          => $paged, 
));