【问题标题】:wp_query pagination on archive.phparchive.php 上的 wp_query 分页
【发布时间】:2018-05-03 01:36:34
【问题描述】:

我在 Wordpress 的几页上使​​用了以下 wp_query,正如您所见,我正在尝试确保查询是分页的。在自定义页面模板 page-articles.php 上一切正常,但是我无法在 archive.php 模板上获得相同的结果。

分页链接渲染成功(例如 mydomain.com/category/my-cat/page/2)但是点击链接不起作用,它只是抛出 404 错误?这些链接怎么可能不去任何地方?

我假设在 archive.php 模板上使用自定义 wp_query 存在一些问题?

谢谢!

查询

$args = array(
    'post_type'         => 'post',
    'posts_per_page'    => 1,
    'paged'             => $paged,
    'orderby'           => 'date',
    'order'             => 'DESC'
);
$articles = new WP_Query($args );
?>

循环

<?php if ( $articles->have_posts() ) : ?>
   <?php while ( $articles->have_posts() ) : $articles->the_post(); ?>
       Posts here!
   <?php endwhile; ?>
   <?php wp_reset_postdata(); ?>
<?php endif; ?>

分页

<nav>
    <div class="prev"><?php echo get_previous_posts_link( 'Previous', $articles->max_num_pages );   ?></div>
    <div class="next"><?php echo get_next_posts_link( 'Next', $articles->max_num_pages ); ?></div>
</nav>

【问题讨论】:

    标签: php wordpress pagination


    【解决方案1】:

    经过一番挖掘,在this article 的帮助下,下面的解决方案解决了这个问题。只需将其放在您的 functions.php 文件中即可。以下实现适用于自定义帖子类型以及类别的存档。

    /**
     * Wordpress has a known bug with the posts_per_page value and overriding it using
     * query_posts. The result is that although the number of allowed posts_per_page
     * is abided by on the first page, subsequent pages give a 404 error and act as if
     * there are no more custom post type posts to show and thus gives a 404 error.
     *
     * This fix is a nicer alternative to setting the blog pages show at most value in the
     * WP Admin reading options screen to a low value like 1.
     *
     */
    function custom_posts_per_page( $query ) {
    
        if ( $query->is_archive('cpt_name') || $query->is_category() ) {
            set_query_var('posts_per_page', 1);
        }
    }
    add_action( 'pre_get_posts', 'custom_posts_per_page' );
    

    【讨论】:

      【解决方案2】:

      是的,是的。

      这是因为在您使用 WP_QUERY 时,archive.php 的主要查询保持不变。尝试在 archive.php 中使用 query_posts()。

      query_posts($args);
      

      然后是默认循环(而不是 $articles)

      <?php if ( have_posts() ) : ?>
         <?php while (have_posts() ) : the_post(); ?>
             Posts here!
         <?php endwhile; ?>
         <?php wp_reset_postdata(); ?>
      <?php endif; ?>
      

      【讨论】:

      • 嗨@elvin85,感谢您的回复。我只是实现了您的解决方案,但它只是输出相同的分页链接(例如example.com/category/adventure/page/2 - 问题实际上似乎是这些页面实际上不存在(尽管有很多帖子可以创建它们)。Wordpress 显然认为他们存在输出链接,但他们不存在?我重置了永久链接并没有帮助?
      • 如果分页链接存在但给出 404 错误,这意味着您需要使用 pre_get_posts() 过滤器并使用 $query->set() 方法设置参数。
      • 好的,您能帮忙调整原始代码以适应这种方法吗?谢谢!
      • 对于您需要添加到您的functions.php 中的那部分有现成的答案。 f.e. stackoverflow.com/a/42191034/701666
      • 谢谢,我不敢相信这么简单的任务需要这么复杂?例如我根本没有使用自定义分页功能?
      猜你喜欢
      • 1970-01-01
      • 2013-04-08
      • 2015-05-21
      • 2013-01-13
      • 2016-09-29
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多