wp_query无限循环无法解析

问题描述 投票:0回答:1

我很欣赏这里有数以百万计的wp_query无限循环问题,我已经查看并试图找到答案,但到目前为止似乎没有什么比较合适!

我试图编写的wp_query应该很简单,但我显然在我的代码中遗漏了一些东西;

 $args = array ('cat' => 2893);

           // The Query
 $the_query = new WP_Query( $args );

           // The Loop
 if ( $the_query->have_posts() ) {
      echo '<ul>';
      while ( $the_query->have_posts() ) {

          echo '<li>' . the_title() . '</li>';

      }

      echo '</ul>';
          /* Restore original Post Data */
      wp_reset_postdata();
  } else {
      echo '<p>nothing</p>';
  }

当我将回声线更改为时,上面的代码会导致标题无限循环

echo '<li>' . $the_query->the_title() . '</li>';

我认为应该解决循环问题,页面加载到查询并挂起,直到达到执行超时。

关于问题可能是什么的任何想法?

php wordpress loops while-loop
1个回答
1
投票

我想你忘了添加the_post()

在循环中迭代post索引。

<?php if ( $the_query->have_posts() ) { the_post(); 

https://developer.wordpress.org/reference/functions/the_post/

© www.soinside.com 2019 - 2024. All rights reserved.