
在网页开发中,我们经常需要将一系列数据项(如文章、产品等)进行分组展示,例如每三项显示为一行。更进一步的需求是,不仅要实现分组,还需要知道每个分组(行)中实际包含的元素数量,并将这个数量动态地体现在该分组的html结构(如css类名)中。例如,一个包含3个元素的行,其外层div可能需要 class="project_row projectitemcount-3";而最后一个分组可能只有2个元素,则需要 class="project_row projectitemcount-2"。传统的通过循环索引 % 运算符来判断开闭标签的方法,难以在关闭标签时准确获取当前分组的元素总数。
为了解决上述问题,我们需要一种机制来“预知”或“暂存”当前分组的元素,以便在生成该分组的外部容器时,能够获取到其内部元素的准确数量。核心策略如下:
以下代码示例将展示如何使用这种策略来动态生成分组并计数。我们将首先提供一个通用的PHP示例,然后将其适配到WordPress环境中。
假设我们有一个包含多个数据项的数组:
<?php
// 模拟的数据源
$items = [
['id' => 1, 'title' => '产品A', 'category' => '电子产品', 'image' => 'https://via.placeholder.com/300x200'],
['id' => 2, 'title' => '产品B', 'category' => '家居用品', 'image' => 'https://via.placeholder.com/300x200'],
['id' => 3, 'title' => '产品C', 'category' => '服饰', 'image' => 'https://via.placeholder.com/300x200'],
['id' => 4, 'title' => '产品D', 'category' => '电子产品', 'image' => 'https://via.placeholder.com/300x200'],
['id' => 5, 'title' => '产品E', 'category' => '家居用品', 'image' => 'https://via.placeholder.com/300x200'],
['id' => 6, 'title' => '产品F', 'category' => '服饰', 'image' => 'https://via.placeholder.com/300x200'],
['id' => 7, 'title' => '产品G', 'category' => '电子产品', 'image' => 'https://via.placeholder.com/300x200'],
['id' => 8, 'title' => '产品H', 'category' => '家居用品', 'image' => 'https://via.placeholder.com/300x200'],
];
$items_per_row = 3; // 每行显示的项目数量
$html_output = ''; // 用于存储生成的HTML
$current_row_items_data = []; // 临时数组,用于暂存当前行的项目数据
$total_items = count($items); // 数据总数
for ($i = 0; $i < $total_items; $i++) {
$item = $items[$i];
$current_row_items_data[] = $item; // 将当前项目添加到临时数组
// 判断是否达到每行项目数限制,或者是否是最后一个项目
if (count($current_row_items_data) === $items_per_row || $i === $total_items - 1) {
$item_count_in_this_row = count($current_row_items_data); // 获取当前行的项目数量
// 输出行容器,包含动态计数类
$html_output .= '<div class="project_row projectitemcount-' . $item_count_in_this_row . '">';
// 遍历临时数组,输出当前行内的每个项目
foreach ($current_row_items_data as $row_item) {
$html_output .= '<div class="project_item">';
$html_output .= '<a href="/item/' . $row_item['id'] . '">';
$html_output .= '<div class="project_item_img"><img src="' . htmlspecialchars($row_item['image']) . '" alt="' . htmlspecialchars($row_item['title']) . '"/></div>';
$html_output .= '<div class="project_item_content">';
$html_output .= '<h3>' . htmlspecialchars($row_item['title']) . '</h3>';
$html_output .= '<p>' . htmlspecialchars($row_item['category']) . '</p>';
$html_output .= '</div>';
$html_output .= '</a>';
$html_output .= '</div>';
}
$html_output .= '</div>'; // 关闭行容器
$current_row_items_data = []; // 重置临时数组,为下一行做准备
}
}
echo $html_output;
?>在WordPress环境中,通常会使用 WP_Query 来获取文章列表。以下是如何将上述逻辑集成到WordPress循环中:
立即学习“PHP免费学习笔记(深入)”;
<?php
// 假设 $custom_query 是一个 WP_Query 对象
// 例如: $custom_query = new WP_Query( array( 'post_type' => 'project', 'posts_per_page' => -1 ) );
if ($custom_query->have_posts()) {
$items_per_row = 3; // 每行显示的项目数量
$html_output = ''; // 用于存储生成的HTML
$current_row_items_data = []; // 临时数组,用于暂存当前行的项目数据
$post_index = 0; // 用于跟踪当前处理到第几个文章(从0开始)
while ($custom_query->have_posts()) {
$custom_query->the_post(); // 设置当前文章数据
// 收集当前文章所需的数据
$post_data = [
'permalink' => get_the_permalink(),
'title' => get_the_title(),
'terms' => wp_get_post_terms(get_the_ID(), 'your_taxonomy_slug', ['fields' => 'names']), // 替换 'your_taxonomy_slug' 为实际分类法
'image_url' => get_the_post_thumbnail_url(get_the_ID(), 'large') ?: 'https://via.placeholder.com/940x1260', // 获取特色图片URL或使用占位符
];
$current_row_items_data[] = $post_data; // 将当前文章数据添加到临时数组
$post_index++; // 递增文章索引
// 判断是否达到每行项目数限制,或者是否是所有文章中的最后一个
if (count($current_row_items_data) === $items_per_row || $post_index === $custom_query->post_count) {
$item_count_in_this_row = count($current_row_items_data); // 获取当前行的文章数量
// 输出行容器,包含动态计数类
$html_output .= '<div class="project_row projectitemcount-' . $item_count_in_this_row . '">';
// 遍历临时数组,输出当前行内的每个文章项目
foreach ($current_row_items_data as $item_data) {
$html_output .= '<div class="project_item">';
$html_output .= '<a href="' . esc_url($item_data['permalink']) . '">'; // 使用 esc_url 进行URL转义
$html_output .= '<div class="project_item_img"><img src="' . esc_url($item_data['image_url']) . '" alt="' . esc_attr($item_data['title']) . '"/></div>'; // 使用 esc_attr 进行属性转义
$html_output .= '<div class="et_pb_text_inner project_item_content">';
$html_output .= '<h3>' . esc_html($item_data['title']) . '</h3>'; // 使用 esc_html 进行HTML内容转义
if (!empty($item_data['terms'])) {
foreach ($item_data['terms'] as $term_name) {
$html_output .= '<p>' . esc_html($term_name) . '</p>';
}
}
$html_output .= '</div>';
$html_output .= '</a>';
$html_output .= '</div>';
}
$html_output .= '</div>'; // 关闭行容器
$current_row_items_data = []; // 重置临时数组,为下一行做准备
}
}
wp_reset_postdata(); // 恢复全局 $post 数据
}
echo $html_output;
?>
.project_row {
display: flex;
flex-wrap: wrap;
margin-bottom: 20px;
}
.project_row .project_item {
flex: 0 0 calc(33.333% - 20px); /* 默认三列 */
margin: 10px;
}
.project_row.projectitemcount-2 .project_item {
flex: 0 0 calc(50% - 20px以上就是PHP循环分组:动态计算并显示每组子元素数量的教程的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号