简体   繁体   English

在Wordpress中对帖子的类别页面进行分页

[英]Paginate a category page of posts in Wordpress

Is it possible to add next and previous post links to a page that is querying posts from one category and displaying one post of that category per page? 是否可以将下一个和上一个帖子链接添加到一个页面,该链接从一个类别查询帖子并在每页显示一个该类别的帖子?

This is what I currently have: 这是我目前拥有的:

        <?php query_posts('cat=2&posts_per_page=1'); ?>

        <?php if (have_posts()): while (have_posts()) : the_post(); ?>

            <!-- article -->
            <article class="overlay" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

                <div class="inner">

                    <div class="gallery" style="background-image: url(<?php the_field('image'); ?>);">
                        <div class="close" data-home="<?php echo home_url(); ?>">
                            <span class="oi" data-glyph="x"></span>
                        </div>
                    </div>

                    <div class="copy">
                        <h2><?php the_title(); ?></h2>
                        <?php the_field('news_content'); ?>
                        <a href="**NEXT_POST_IN_SAME_CATEGORY**">Next</a>

                    </div>

                </div>

            </article>
            <!-- /article -->

        <?php endwhile; ?>

您可以尝试使用next_post_link()

 <?php next_posts_link(); ?>

This seemed to be the only solution that worked for me, if anyone needs a template: 如果有人需要模板,这似乎是唯一对我有用的解决方案:

<?php get_header(); ?>

    <main role="main">
    <!-- section -->
    <section>

    <?php if (have_posts()): while (have_posts()) : the_post(); ?>

        <!-- article -->
        <article class="overlay" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

            <div class="inner">

                <div class="copy">
                    <h2><?php the_title(); ?></h2>
                    <?php the_field('news_content'); ?>
                    <?php the_field('copy'); ?>
                    <br>
                    <br>
                    <?php next_post_link( '%link', 'Next', TRUE, 'post_format' ); ?>  |  <?php previous_post_link( '%link', 'Previous', TRUE, 'post_format' ); ?>
                </div>

            </div>

        </article>
        <!-- /article -->

    <?php endwhile; ?>

    <?php else: ?>

        <!-- article -->
        <article>

            <h1><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h1>

        </article>
        <!-- /article -->

    <?php endif; ?>

    </section>
    <!-- /section -->
    </main>

<?php get_footer(); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM