【问题标题】:Troubleshooting Wordpress PaginationWordPress 分页疑难解答
【发布时间】:2021-11-25 14:25:20
【问题描述】:

我目前正在尝试让我的 Wordpress 系统中的分页工作,但由于 WP_Query 属性的一些奇怪问题,它似乎总是失败,一旦使用全局属性,max_num_pages 就会返回 0。

对于我用来获取所需帖子的查询,max_num_pages 属性设置正确。 这是我用来获取帖子的查询:

return new WP_Query(
        array(
            'post_type' => 'dokumente',
            'post__in' => $documentIds,
            'no_found_rows'          => true,
            'posts_per_page'         => 10,
        ));

这是我尝试用于呈现页面的代码:

if ($documentQuery->have_posts()) {
while ($documentQuery->have_posts()) : $documentQuery->the_post();
    global $wp_query;
    global $post;
    $document = $post;
    
endwhile;
?>
<div class="nav-previous alignleft"><?php next_posts_link('Older posts'); ?></div>


<div class="nav-next alignright"><?php previous_posts_link('Newer posts'); ?></div>
<?php

}

查询作为“$documentQuery”传递给函数。

如果有人可以在这里帮助我,那就太好了。从今天开始,我就被困在这个问题上。

【问题讨论】:

  • global $wp_query; 首先在那里做什么?这甚至不是您在那里循环的查询,而是$documentQuery
  • 您应该简单地将$documentQuery-&gt;max_num_pages 作为第二个参数传递给next_posts_link
  • @CBroe 是的,这就是我现在解决它的方法。感谢您的提示!不知道这是可能的。

标签: php wordpress wordpress-plugin-creation


【解决方案1】:

正如@CBroe 所指出的,我现在将$documentQuery-&gt;max_num_pages 作为第二个参数传递给next_posts_link。 对于所有依赖于global $wp_query 属性的通用函数,在调用依赖$wp_query 的函数之前,我已将其替换为我的$documentQuery

【讨论】:

    猜你喜欢
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 2014-08-29
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多