WordPress single.php循环末尾PHP语法错误求助
Hey there! That syntax error you're seeing is happening because you used PHP's alternative control structure syntax (using colons : instead of curly braces {), but forgot to close the outer if statement that wraps your while loop.
Let me break it down clearly: You started with <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> — this sets up two nested control structures: an if check and a while loop. You correctly closed the while loop with <?php endwhile; ?>, but never added the matching endif; to wrap up the original if condition.
Here's your corrected code, with the missing endif; added, plus a fix for an unclosed <div> that would have caused HTML rendering issues:
<?php get_header(); ?> <div class="container"> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <div class="row"> <div class="col-sm-5"> <div class="image_cont img_width_sgl" style= "background: url(<?php the_field('post_image'); ?>) no-repeat center center; background-size: cover;"> </div> </div> </div> <!-- Added closing div for the first row --> <div class="row"> <div class="col-sm-5"> <h2><?php the_title();?></h2> <h3><?php the_time('l, F jS, Y'); ?></h3> </div> </div> <div class="row"> <div class="news_cont"> <?php the_content();?> <p class="back_btn_news"> <a href="http://vplc.rpmdevserver.com/news/" class="">BACK TO NEWS</a> </p> </div> </div> <div class="row"> <div class="col-sm-5"> <?php comments_template(); ?> </div> </div> <?php endwhile; endif; ?> <!-- Added missing endif; to close the outer if --> </div> <!-- Closing the container div -->
A quick best practice tip for PHP's alternative syntax: Always make sure every opening if :, while :, or foreach : has a corresponding endif;, endwhile;, or endforeach;. It's super easy to miss these when nesting structures, so double-checking can save you from these frustrating syntax errors!
内容的提问来源于stack exchange,提问作者sanzanobi

