You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级PHP至8.0遇endwhile语法解析错误,求修复方案

PHP 8.0升级后模板出现Parse error的修复方案

问题详情

近期将网站PHP版本升级至8.0后,某WordPress模板报错:
Parse error: syntax error, unexpected token "endwhile" on line 118.
回退到PHP 7.4可正常运行,但该版本即将停止维护,相关模板代码如下:

/**
 * The template for displaying all single posts and attachments
 *
 * @package WordPress
 * @subpackage Twenty_Sixteen
 * @since Twenty Sixteen 1.0
 */

get_header('projects');

while (have_posts()) : the_post();
    
    $visibilty = get_post_meta($post->ID, '_visibilty', true);
    
    if ($visibilty == 'yes') {
        ?>
        
        <h2 style="text-align:center; padding:90px 0;">Sorry this is Confidential Page</h2>
        <?php
        exit();
    }
    $thebgimage = '';
    if(function_exists('top_image_get_meta')){
        $thebgimage = wp_get_attachment_image_src(top_image_get_meta('_top_image_id'), 'full');
    }
    
    $vimeovideoid = get_field('project_video_vimeo_id', $post->ID);
    $self_hosted_mp4 = get_field('project_video_sh_mp4', $post->ID);
    $self_hosted_webm = get_field('project_video_sh_webm', $post->ID);
    $self_hosted_still = get_field('project_video_sh_still', $post->ID);
    
    if (isset($vimeovideoid) && !empty($vimeovideoid)) { ?>
        
        <div class="work-top">
            <div class="grid">
                <div class="grid-sizer"></div>
                <div class="grid-item grid-item--width3 grid-item--height2">
                    <div style="position:absolute;bottom:0px;left:0;z-index:-10;width:100vw;height:1200px;">
                        <div class="vimeo-wrapper">
                            <iframe class="gridvid" src="https://player.vimeo.com/video/<?php echo $vimeovideoid; ?>?background=1&autoplay=1&loop=1&byline=0&title=0" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <?php
    } elseif (isset($self_hosted_mp4) && !empty($self_hosted_mp4) && isset($self_hosted_still) && !empty($self_hosted_still)) {
        ?>
        
        <div class="work-top">
            <div class="grid">
                <div class="grid-sizer"></div>
                <div class="grid-item grid-item--width3 grid-item--height2">
                    <div class="vid-wrapper" style="position:absolute;bottom:0;left:0;z-index:-10;width:100vw;overflow:hidden;">
                        <video autoplay playsinline muted id="v2projectvideo" class="gridvidself" poster="<?php echo $self_hosted_still; ?>" style="opacity:1;object-fit:fill;display:block;position:absolute;left:50%;transform:translate(-50%, 0);" loop>
                            <source src="<?php echo $self_hosted_mp4; ?>" type="video/mp4"><?php if (isset($self_hosted_webm) && !empty($self_hosted_webm)) { ?>
                                <source src="<?php echo $self_hosted_webm; ?>" type="video/webm"><? } ?></video>
                    </div>
                </div>
            </div>
        </div>
        
        <?php
    } elseif (substr($thebgimage[0], -13) !== 'get_start.jpg' && $thebgimage[0] != '') {
        ?>
        <div class="work-top">
            <div class="grid">
                <div class="grid-sizer"></div>
                <div class="grid-item grid-item--width3 grid-item--height2">
                    <div class="grid-inner" style="background-image:url(<?php echo $thebgimage[0]; ?>);"></div>
                </div>
            </div>
        </div>
        <?php
    } elseif (has_post_thumbnail()) {
        $bg_img = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_id()), "full");
        
        ?>
        
        <div class="work-top">
            <div class="grid">
                <div class="grid-sizer"></div>
                <div class="grid-item grid-item--width3 grid-item--height2">
                    <div class="grid-inner" style="background-image:url(<?php echo $bg_img[0]; ?>);"></div>
                </div>
            </div>
        </div>
    <?php } ?><?php
    $theprojectclientname = get_post_meta($post->ID, '_client_name', true);
    if ($theprojectclientname != '') { ?>
        <section class="kc_row project-text ptop90 clearfix">
            <div class="kc-row-container">
                <div class="kc-wrap-columns">
                    <div class="kc-elm kc-css-891324 kc_col-sm-3 kc_column kc_col-sm-3">
                        <div class="kc-col-container"></div>
                    </div>
                    <div class="kc-elm kc-css-119619 kc_col-sm-6 kc_column kc_col-sm-6">
                        <div class="kc-col-container">
                            <div class="kc-elm kc-css-609049 kc_text_block">
                                <p class="projectclient">CLIENT
                                    <span class="projectclientname"><?php echo $theprojectclientname; ?></span></p>
                                <p class="projectheadline"><?php echo get_post_meta($post->ID, '_client_headline', true); ?></p>
                            </div>
                        </div>
                    </div>
                    <div class="kc-elm kc-css-144440 kc_col-sm-3 kc_column kc_col-sm-3">
                        <div class="kc-col-container"></div>
                    </div>
                </div>
            </div>
        </section>
    <?php } ?>
    <div class="work-body">
        <?php the_content(); ?>
    </div>
    <?php //echo do_shortcode('[Related_projects]'); ?><?php endwhile; ?>
<?php //get_sidebar(); ?>
<?php get_footer(); ?>

错误原因

PHP 8.0对语法严谨性要求提升,原代码中exit()语句直接终止脚本执行,导致while循环的endwhile标记无法匹配对应的while (have_posts()) :起始结构。PHP 7.4容忍这种不完整的循环结构,但PHP 8.0会直接抛出语法错误。

修复方法

将exit()替换为WordPress官方推荐的wp_die()函数,它会安全终止脚本同时保证语法结构完整:

if ($visibilty == 'yes') {
    ?>
    <h2 style="text-align:center; padding:90px 0;">Sorry this is Confidential Page</h2>
    <?php
    wp_die(); // 替换原有的exit()
}

额外优化建议

代码中$thebgimage[0]的调用存在风险:当top_image_get_meta函数不存在时,$thebgimage为空字符串,直接取索引会触发Warning。建议修改判断逻辑:

// 原判断
// elseif (substr($thebgimage[0], -13) !== 'get_start.jpg' && $thebgimage[0] != '') {

// 修改后
elseif (!empty($thebgimage) && substr($thebgimage[0], -13) !== 'get_start.jpg') {

内容的提问来源于stack exchange,提问作者user1652997

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 06:40:51