如何在 PHP 中循环显示 WordPress 帖子的 while 语句?

我创建了一个 PHP while 语句,它以特定布局显示来自 WordPress 的 8 个帖子。现在我想继续运行这个循环,直到显示所有帖子。所以我所做的是创建一个名为posts_displayed 的变量,我使用它来设置post_offset 并在每次显示帖子时增加它。原始代码按预期工作,但是当我在它周围添加额外的 while 语句时,没有显示任何帖子。

我已经更新了代码,以便有一个主查询和循环,其中包含四个不同的查询和循环。我现在遇到的问题是在第 131 行,在那里我遇到了意外的结束时错误。


红颜莎娜
浏览 171回答 1
1回答

眼眸繁星

我看到您的代码有一些问题。首先在你的第一个如果if ( $the_query->have_posts() )$the_query 仍未定义,因此它没有帖子,您将 $the_query 下面的几行代码定义为 WP_Query 类的实例。其次,您wp_reset_postdata()有条件地调用,这意味着如果查询没有帖子发布数据将不会被重置。当然,您查询数据库的次数太多了,我不明白您为什么需要它。我做了一些快速更正(注意下面的代码未经测试,我只是应用了我能想到的快速解决方案)<?php$the_query = new WP_Query(&nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; 'category_name'&nbsp; => 'Travel',&nbsp; &nbsp; &nbsp; &nbsp; 'posts_per_page' => 64,&nbsp; &nbsp; ));?><div class="row row__padding--bottom">&nbsp; &nbsp; <?php&nbsp; &nbsp; if ( $the_query->have_posts() ) :&nbsp; &nbsp; &nbsp; &nbsp; $i&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= 0;&nbsp; &nbsp; &nbsp; &nbsp; $class_map = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 => 'col-md-6',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 => 'col-md-6',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 6 => 'col-md-6',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 7 => 'col-md-6',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2 => 'col-md-4',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3 => 'col-md-4',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4 => 'col-md-4',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5 => 'col-md-12',&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; while ( $the_query->have_posts() ) :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $the_query->the_post();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-12 <?php echo esc_attr( $class_map[ $i % 8 ] ); ?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="journal__featured" style="background: url(<?php the_post_thumbnail_url( 'large' ); ?>) !important; !important; background-size: cover !important; background-position: center center !important; background-repeat: no-repeat !important;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="post__info--container">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( 1 === ($i % 8) || 4 === ($i % 8) || 5 === ($i % 8) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '</div><div class="row row__padding--bottom">';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i++;&nbsp; &nbsp; &nbsp; &nbsp; endwhile;&nbsp; &nbsp; else :&nbsp; &nbsp; &nbsp; &nbsp; echo '<p>' . esc_html( __( 'No News' ) ) . '</p>';&nbsp; &nbsp; endif;&nbsp; &nbsp; wp_reset_postdata();&nbsp; &nbsp; ?></div><?php希望这对你有用!
打开App,查看更多内容
随时随地看视频慕课网APP