为什么WordPress循环永远不会结束?

时间:2017-03-28 06:33:33

标签: php wordpress

我在PHP7 / XAMPP服务器上运行WordPress,我正在尝试制作一个简单的“主题”这是主题的代码。

代码清单1:header.php

<!DOCTYPE html>
<html lang="en">
    <head>

        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">

        <title>Blog Post</title>

    </head>
    <body>

代码清单2:index.php

<?php 
    get_header();
?>

    <!-- Page Content -->
    <div class="container">

        <div class="row">

            <!-- Blog Post Content Column -->
            <div class="col-lg-8">
                <!-- Blog Posts -->
                <?php
                    if(have_posts()){
                        while(have_posts){
                            the_post();
                            if(the_post_thumbnail()){
                                the_post_thumbnail( 'full' );
                            }
                            the_content();
                        }
                    }
                ?>

            </div>

        </div>

    </div>

<?php 
    get_footer();
?>

代码清单3:footer.php

        <!-- Footer -->
        <footer>
            <div class="row">
                <div class="col-lg-12">
                    <p>Copyright &copy; Your Website 2017</p>
                </div>
            </div>
            <!-- /.row -->
        </footer>

    </body>
</html>

代码清单4:functions.php

<?php

    add_theme_support("post-thumbnails");

每当我加载页面时,我都会收到此PHP错误:

`Fatal error: Maximum execution time of 30 seconds exceeded in E:\W`WW\wordpress473\wp-includes\formatting.php on line 4786

发生错误的行可能不同,但错误始终发生在同一脚本上。 在加载页面时,循环会一遍又一遍地加载相同的帖子,直到PHP停止脚本。

1 个答案:

答案 0 :(得分:2)

不确定wordpress,但这一行在这里:

if(have_posts()){
   while(have_posts){
      the_post();
         if(the_post_thumbnail()){
            the_post_thumbnail( 'full' );
         }
      the_content();
   }
}

您正在检查&#34; have_posts()&#34;的结果并且你在循环中使用&#34; have_posts&#34;。如果它是返回Iterable的函数,那么你应该修复那个拼写错误并循环遍历&#34; have_posts()&#34;在while循环中。

这是我在文档中找到的内容,看起来像我一样严谨:

if ( have_posts() ) :
while ( have_posts() ) : the_post();
    // Your loop code
endwhile;  
else :
_e( 'Sorry, no posts were found.', 'textdomain' );

ENDIF;