为什么next_posts_link和previous_posts_link不起作用?

时间:2014-12-12 19:07:35

标签: wordpress

我尝试在自定义帖子类型模板中创建下一页和上一页链接。我认为它应该有效,但它不是。当我检查开发人员工具时,.pagination类的列表项中没有链接。

enter image description here

我做错了什么?

<section class="page-title">
    <div class="container_12">
        <header class="grid_12">
            <h2><?php the_title(); ?></h2>
        </header><!-- End header.grid_12 -->

    <div class="clear"></div><!-- End div.clear -->
    </div><!-- End div.container_12 -->
</section><!-- End section.page-title -->


<section class="page entry">
    <div class="container_12">

            <?php
                $args = array('post_type' => 'roosters', 'posts_per_page' => 10, 'paged' => $paged);
                $loop = new WP_Query($args);
                $count = 0;
            ?>

            <?php if ($loop) : while ($loop->have_posts()) : $loop->the_post(); ?>
            <article class="grid_12 post-entry">
                <header>
                    <h4><a href="<?php the_permalink(); ?>" title=""><?php the_title(); ?></a></h4>
                    <?php the_excerpt(); ?>
                </header><!-- End header -->

                <footer class="meta">
                    <p><?php the_date(); ?> - Geplaatst door: <?php the_author(); ?></p>
                </footer><!-- End footer.meta -->

            </article><!-- End article.grid_12 post-entry -->
            <?php endwhile; ?>
            <?php else : ?>
                <p>No posts were found! I'm not sure what you're looking for.</p>
            <?php endif; ?>
            <?php wp_reset_query(); ?>


            <footer class="grid_12 center">
                <ul class="pagination">
                    <li class="prev-page">
                        <?php previous_posts_link('&larr; Vorige pagina') ?>
                    </li>

                    <li class="next-page">
                        <?php next_posts_link('Volgende pagina &rarr;'); ?>
                    </li>

                </ul><!-- End ul.pagination -->

            </footer><!-- End footer.grid_12 center -->

    <div class="clear"></div><!-- End div.clear -->
    </div><!-- End div.container_12 -->
</section><!-- End section.page entry -->

2 个答案:

答案 0 :(得分:0)

编辑: 这最终成了修复:

<?php next_posts_link('Volgende pagina &rarr;', $loop->max_num_pages); ?> 

原始答案,未解决以下问题: 尝试将wp_reset_query()移动到您使用下一个和上一个帖子链接的位置。

例如:

            </article><!-- End article.grid_12 post-entry -->
        <?php endwhile; ?>
        <?php else : ?>
            <p>No posts were found! I'm not sure what you're looking for.</p>
        <?php endif; ?>



        <footer class="grid_12 center">
            <ul class="pagination">
                <li class="prev-page">
                    <?php previous_posts_link('&larr; Vorige pagina') ?>
                </li>

                <li class="next-page">
                    <?php next_posts_link('Volgende pagina &rarr;'); ?>
                </li>

            </ul><!-- End ul.pagination -->

        </footer><!-- End footer.grid_12 center -->
        <?php wp_reset_query(); ?>

答案 1 :(得分:0)

您的代码存在一些问题

  • $paged未定义。你需要定义它

    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    
  • wp_reset_query();用于重置query_posts(您应该从不>使用)。使用wp_reset_postdata()

  • 时,您应该使用WP_query
  • 您应该在wp_reset_postdata()之前使用endif,而不是之后。如果您的自定义查询中没有帖子,您将重置主查询

  • 使用自定义查询时,您需要在$max_pages中设置next_posts_link参数,这样您就可以执行此类操作

    next_posts_link('Volgende pagina &rarr;', $loop->max_num_pages);
    
  • 您不应在结束页脚标记后使用<?php wp_reset_query(); ?>。你可以删除。完全没必要