WordPress have_posts仅返回一页

问题描述

我正在尝试建立一个小的WordPress网站,但发现自己陷于困境:

my posts. I have 5 of them.

我想在页面显示我的帖子,不带循环:

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

    <!-- article -->
    <article id="post-<?PHP the_ID(); ?>" <?PHP post_class(); ?>>

        <!-- post title -->
        <h2>
            <a href="<?PHP the_permalink(); ?>" title="<?PHP the_title(); ?>"><?PHP the_title(); ?></a>
        </h2>
        <!-- /post title -->

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

<?PHP endwhile; ?>

在我的页面中执行此操作只会返回一个元素,这甚至不是帖子,而是页面

result of the code above

我很惊讶,因为这看起来很琐碎。所以我 编写了一些代码来调试它:

$a = get_posts();

echo("len : " . sizeof($a) . "<br/></br>");

foreach($a as $article){

    echo($article->post_title . "<br/>");
}

result of above code

对我而言,这完全没有道理。在过去的几个小时中,我一直在使用示例而不是其他方法进行检查和尝试,但无济于事....

编辑1:一句话,经典的have_posts()循环仅返回一页而不是所有帖子

谢谢。

解决方法

好的,我找到了 a 解决方案:

$allPostsWPQuery = new WP_Query(array('post_type'=>'post','post_status'=>'publish','posts_per_page'=>-1));
if ($allPostsWPQuery->have_posts()): while ($allPostsWPQuery->have_posts()) : $allPostsWPQuery->the_post(); ?>

使用此自定义查询替换循环块的开始

aaaaaand:

result

并且不要忘记在wp_reset_query();

末尾删除此自定义查询

编辑:感谢评论,我知道这是预期的行为。