【问题标题】:get_posts not returning all postsget_posts 不返回所有帖子
【发布时间】:2012-08-14 04:19:03
【问题描述】:

我必须手动挂载博客文章,但我不确定这是否是正确的工作方式,它只有 9 页,每个页面有 4 个帖子,但博客有 83 个帖子!

<?php
    $paged = get_query_var('paged');
    $args = array(
        'numberposts'       => 4,
        'offset'            => $paged*4,
        'orderby'           => 'post_date',
        'order'             => 'DESC',
        'post_type'         => 'post',
        'post_status'       => 'publish',
        'suppress_filters'  => true
    );
    $posts_array = get_posts( $args );
?>

还是谢谢。

【问题讨论】:

  • 仔细检查您所有的 83 篇帖子都“已发布”。
  • 是的,它们都已发布,但我注意到它只带来 'no category' 的帖子,我怎样才能获得包括其他类别的所有帖子?

标签: wordpress


【解决方案1】:

问题是您的“numberposts”设置为 4 将其设置为 -1 以获取所有帖子:

 'numberposts'       => -1,

如果您未在此处设置帖子数,WordPress 将从您的仪表板设置中提取帖子数(在设置 -> 阅读下)

【讨论】:

  • 人...你节省了我的一天..谢谢..顺便说一下,我没有设置 numberposts ..但它具有 WP 的默认值,所以我将其设置为 -1 以强制 WP 获取它
【解决方案2】:

以下注释来自此codex section

注意:使用偏移量时,上述查询只能用于 包含多个帖子的类别,否则将没有 输出。

所以为了显示所有帖子,每个类别至少应该有 2 个帖子。

您可以尝试Loops 获取所有帖子。也检查The Loop in Action

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多