首页 > 解决方案 > PHP 循环和 For 循环

问题描述

我原以为这是直截了当的,但我对 The Loop 的理解是有限的。

我的 WordPress 网站上有大约 6 篇博文。但是,我只想在主页上显示其中的 2 个。

我认为嵌套在 For 循环中的循环会起作用,但无济于事。

下面是我的代码,这是行不通的。它循环 6 个帖子 3 次。所以它输出了 18 个帖子,而不是预期的 2 个。

<?php

      for($x = 0; $x<=2; $x++) {
      while(have_posts()) {

        the_post(); ?>

      <h2 class="blog-post-title"><?php the_title(); ?></h2>
        <p class="blog-post-meta text-muted">
          <?php the_time('F Y'); ?> by  <a href="#"><?php the_author_posts_link(); ?> in <?php echo get_the_category_list(', '); ?></a>
        </p>

        <p><?php the_content(); ?></p>

        <?php 

       }} ?>

标签: php

解决方案


推荐阅读