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

WordPress+ACF自定义PHP区块重复输出问题求助

问题:WordPress ACF自定义区块课程列表重复输出

问题描述

使用WordPress、ACF和PHP开发自定义区块,当前代码功能正常,但存在课程列表重复输出的问题。期望输出的课程列表为:

  • APCS 207 Command Line Interface...
  • APCS 242 Computer & Network Management...
  • BUSI 270 Accounting Software Applications...
  • BUSI 353 Advanced Accounting I...

问题分析

原代码核心问题是嵌套循环逻辑错误:在course_list的行循环内部,又对选中的学科代码做了foreach遍历,导致每一行课程会根据选中的代码数量重复输出。同时存在两处细节问题:

  1. 模态框ID中的课程编号未使用echo输出,导致ID不完整
  2. 计数器$i位置不合理,无法保证模态框ID唯一性

修正后的代码

<table style="width: 800px;">
<thead>
    <tr>
        <th>Course</th>
        <th>Course Name</th>
        <th>Course Credits</th>
    </tr>
</thead>
<tbody>
<?php 
$rawcourses = get_field('course_selector'); 
$progcode_check_checked_values = get_field('prog_discipline_code'); 
$i = 0;

foreach($rawcourses as $rawcourse) : 
    if( have_rows('course_list', $rawcourse) ): 
        // 遍历课程列表行
        while( have_rows('course_list', $rawcourse) ) : the_row(); 
            // 获取当前课程的学科代码
            $course_discipline_code = get_sub_field('discipline_code');
            // 仅当当前课程代码在选中列表中时才输出
            if ($progcode_check_checked_values && in_array($course_discipline_code, $progcode_check_checked_values)) : 
                $course_number = get_sub_field('course_number');
                $modal_id = "modal{$course_number}{$i}";
?>
                <tr>
                    <td>
                        <a href="#" data-bs-toggle="modal" data-bs-target="#<?php echo $modal_id; ?>">
                            <?php echo strtoupper($course_discipline_code); ?> <?php echo $course_number; ?>
                        </a>
                    </td>
                    <td><?php echo get_the_title($rawcourse); ?></td>
                    <td><?php the_sub_field('credits'); ?></td>
                </tr>
                <!-- 模态框移出tr标签,符合HTML规范 -->
                <div class="modal fade" id="<?php echo $modal_id; ?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                  <div class="modal-dialog">
                    <div class="modal-content">
                      <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel"><?php echo get_the_title($rawcourse); ?></h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                      </div>
                      <div class="modal-body">
                        <?php 
                        $content_post = get_post($rawcourse);
                        $content = $content_post->post_content;
                        $content = apply_filters('the_content', $content);
                        $content = str_replace(']]>', ']]&gt;', $content);
                        echo $content; 
                        ?>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                      </div>
                    </div>
                  </div>
                </div>
<?php
                $i++;
            endif; 
        endwhile; 
    endif;
endforeach; ?>
</tbody>
</table>

关键修改点

  1. 移除冗余嵌套循环:将学科代码遍历改为in_array判断,确保每个课程仅输出一次
  2. 修复模态框ID:拼接课程编号与计数器生成唯一ID,确保所有变量都通过echo输出
  3. 调整模态框位置:将模态框移出<tr>标签,符合HTML结构规范
  4. 优化逻辑判断:直接对比当前课程代码与选中列表,避免无意义的遍历

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 05:54:58