静态页面上的WP_Query中的分页

时间:2015-08-31 作者:JoshMB

我有一个静态页面,我想在该页面上显示博客帖子列表。我希望每页12篇帖子(因此我希望列表分页)。

除了分页之外,一切都很好。这是我的代码:

    <section id="blogposts" class="content-block content-3-5 blogposts">
            <div class="row">
                <!-- Feature Box 1 -->
                <?php
                    $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
                    $blogposts_args = array(
                        \'post_type\' => \'post\',
                        \'post_status\' => \'publish\',
                        \'nopaging\' => false,
                        \'order\' => \'DESC\',
                        \'orderby\' => \'date\',
                        \'posts_per_page\' => \'12\',
                        \'paged\' => $paged
                    )
                    ?>
                <?php $blogposts = new WP_Query( $blogposts_args ); ?>
                <?php if ( $blogposts->have_posts() ) : ?>
                    <?php $blogposts_item_number = 0; ?>
                    <?php while ( $blogposts->have_posts() ) : $blogposts->the_post(); ?>
                    <?php
                        $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'thumbnail\' );
                        $url = $thumb[\'0\'];
                    ?>
                        <div class="col-md-3 col-sm-6 col-xs-12">
                            <div class="feature-box">
                                <div class="column-item-category"><?php the_category(); ?></div>
                                <a class="feature-box-link" href="<?php the_permalink(); ?>">
                                    <?php the_post_thumbnail( \'\', array(
                                            \'class\' => \'column-circle\'
                                        ) ); ?>
                                </a>
                                <a class="feature-box-link" href="<?php the_permalink(); ?>">
                                    <h4 class="column-item-title"><?php the_title(); ?></h4>
                                </a>
                                <p class="column-item-author"><?php the_author(); ?></p>
                            </div>
                        </div>
                        <?php $blogposts_item_number++; ?>
                        <?php if( $blogposts_item_number % 4 == 0 ) echo \'<div class="clearfix visible-md-block visible-lg-block"></div>\'; ?>
                        <?php if( $blogposts_item_number % 2 == 0 ) echo \'<div class="clearfix visible-sm-block"></div>\'; ?>
                        <?php if( $blogposts_item_number % 1 == 0 ) echo \'<div class="clearfix visible-xs-block"></div>\'; ?>
                    <?php endwhile; ?>
                    <?php wp_reset_postdata(); ?>
                    <div class="post-nav-links">
                        <div class="prev-post"><?php previous_posts_link(); ?></div>
                        <div class="next-post"><?php next_posts_link(); ?></div>
                    </div>
                    <?php else : ?>
                    <p><?php _e( \'Sorry, no posts matched your criteria.\' ); ?></p>
                <?php endif; ?>
                <!-- Feature Box 2 -->
                <!-- Feature Box 3 -->
                <!-- Feature Box 4 -->
            </div>
            <!-- Row Ends -->
    </section>
就像我说的,一切都很好。除非没有显示分页链接。我冒着完全尴尬的风险发布这篇文章,因为我很肯定我错过了一些真正基本的东西。但我正竭尽全力想弄明白,现在似乎是问这个问题的时候了。无论如何非常感谢您的帮助。

1 个回复
最合适的回答,由SO网友:terminator 整理而成

我对您的代码进行了一些编辑。我认为这应该奏效。您需要将参数传递给上一个\\u posts\\u link和下一个\\u posts\\u link。

因此,请尝试以下代码

<section id="blogposts" class="content-block content-3-5 blogposts">
            <div class="row">
                <!-- Feature Box 1 -->
                <?php
                    $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
                    $blogposts_args = array(
                        \'post_type\' => \'post\',
                        \'post_status\' => \'publish\',
                        \'nopaging\' => false,
                        \'order\' => \'DESC\',
                        \'orderby\' => \'date\',
                        \'posts_per_page\' => \'12\',
                        \'paged\' => $paged
                    )
                    ?>
                <?php $blogposts = new WP_Query( $blogposts_args ); ?>
                <?php if ( $blogposts->have_posts() ) : ?>
                    <?php $blogposts_item_number = 0; ?>
                    <?php while ( $blogposts->have_posts() ) : $blogposts->the_post(); ?>
                    <?php
                        $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'thumbnail\' );
                        $url = $thumb[\'0\'];
                    ?>
                        <div class="col-md-3 col-sm-6 col-xs-12">
                            <div class="feature-box">
                                <div class="column-item-category"><?php the_category(); ?></div>
                                <a class="feature-box-link" href="<?php the_permalink(); ?>">
                                    <?php the_post_thumbnail( \'\', array(
                                            \'class\' => \'column-circle\'
                                        ) ); ?>
                                </a>
                                <a class="feature-box-link" href="<?php the_permalink(); ?>">
                                    <h4 class="column-item-title"><?php the_title(); ?></h4>
                                </a>
                                <p class="column-item-author"><?php the_author(); ?></p>
                            </div>
                        </div>
                        <?php $blogposts_item_number++; ?>
                        <?php if( $blogposts_item_number % 4 == 0 ) echo \'<div class="clearfix visible-md-block visible-lg-block"></div>\'; ?>
                        <?php if( $blogposts_item_number % 2 == 0 ) echo \'<div class="clearfix visible-sm-block"></div>\'; ?>
                        <?php if( $blogposts_item_number % 1 == 0 ) echo \'<div class="clearfix visible-xs-block"></div>\'; ?>
                    <?php endwhile; ?>
                    <?php wp_reset_postdata(); ?>
                    <div class="post-nav-links">
                        <div class="prev-post"><?php previous_posts_link(\'Newer\', $blogposts->max_num_pages ); ?></div>
                        <div class="next-post"><?php next_posts_link(\'Older\', $blogposts->max_num_pages ); ?></div>
                    </div>
                    <?php else : ?>
                    <p><?php _e( \'Sorry, no posts matched your criteria.\' ); ?></p>
                <?php endif; ?>
                <!-- Feature Box 2 -->
                <!-- Feature Box 3 -->
                <!-- Feature Box 4 -->
            </div>
            <!-- Row Ends -->
    </section>

相关推荐

Can't Add Pagination

我无法添加分页来在页面之间划分帖子并在页面之间导航。目前所有20篇帖子我都有一次加载。我想在我的页面底部添加分页,并在5之前查看帖子。我有一个自定义的帖子类型,我将其定义为Project。我有两页。主页和日志。主页以我的项目帖子类型为特征,日志以我的常规博客帖子为特征。我希望这两页都有分页。我的当前索引。php是我的主页,如下所示:<?php get_header(); ?> <?php get_footer(); ?> <div class=\"p