如何控制网站首页页脚的分类与子分类显示数量?
当然可以实现,下面给你三种实用的解决方案,你可以根据自己的需求选择:
方法1:限制显示前N个分类(简单直接)
这种方法适合快速控制显示数量,直接在循环时截取数组或用计数器终止循环:
方式A:用array_slice截取分类数组
<?php // 只取前5个分类,可根据需求修改数字5 $limitedCategories = array_slice($getCategories, 0, 5); foreach ($limitedCategories as $item): ?> <div> <ul class="ul-nav ul-nav-default"> <li class="ul-nav-header"><a class="ul-text-secondary ul-text-capitalize" href="<?php echo $urlPath->search(['category' => $item['category_slug']]); ?>"><?php echo echoOutput($item['category_title']); ?></a></li> <?php $getSubCategories = getSubCategories($connect, $item['category_id']); ?> <?php // 同样限制子分类数量,比如前3个 $limitedSubCategories = array_slice($getSubCategories, 0, 3); foreach ($limitedSubCategories as $subItem): ?> <li><a href="<?php echo $urlPath->search(['subcategory' => $subItem['subcategory_slug']]); ?>"><?php echo echoOutput($subItem['subcategory_title']); ?></a></li> <?php endforeach ?> </ul> </div> <?php endforeach ?>
方式B:用计数器控制循环次数
<?php $count = 0; $maxCategories = 5; // 最多显示5个分类 foreach ($getCategories as $item): if($count >= $maxCategories) break; ?> <div> <ul class="ul-nav ul-nav-default"> <li class="ul-nav-header"><a class="ul-text-secondary ul-text-capitalize" href="<?php echo $urlPath->search(['category' => $item['category_slug']]); ?>"><?php echo echoOutput($item['category_title']); ?></a></li> <?php $getSubCategories = getSubCategories($connect, $item['category_id']); ?> <?php $subCount = 0; $maxSubs = 3; // 每个分类最多显示3个子分类 foreach ($getSubCategories as $subItem): if($subCount >= $maxSubs) break; ?> <li><a href="<?php echo $urlPath->search(['subcategory' => $subItem['subcategory_slug']]); ?>"><?php echo echoOutput($subItem['subcategory_title']); ?></a></li> <?php $subCount++; endforeach ?> </ul> </div> <?php $count++; endforeach ?>
方法2:给分类加自定义标记(灵活可控)
如果想自由选择哪些分类显示在页脚,建议在数据库的categories表中添加一个字段(比如is_show_in_footer,类型为布尔值或tinyint),然后修改查询逻辑只获取需要显示的分类:
步骤1:修改分类查询SQL
-- 只查询标记为显示在页脚的分类,可选LIMIT限制数量 SELECT * FROM categories WHERE is_show_in_footer = 1 LIMIT 5;
步骤2:前端代码适配(确保$getCategories是筛选后的结果)
<?php foreach ($getCategories as $item): ?> <div> <ul class="ul-nav ul-nav-default"> <li class="ul-nav-header"><a class="ul-text-secondary ul-text-capitalize" href="<?php echo $urlPath->search(['category' => $item['category_slug']]); ?>"><?php echo echoOutput($item['category_title']); ?></a></li> <?php $getSubCategories = getSubCategories($connect, $item['category_id']); // 同样限制子分类数量 $limitedSubCategories = array_slice($getSubCategories, 0, 3); foreach ($limitedSubCategories as $subItem): ?> <li><a href="<?php echo $urlPath->search(['subcategory' => $subItem['subcategory_slug']]); ?>"><?php echo echoOutput($subItem['subcategory_title']); ?></a></li> <?php endforeach ?> </ul> </div> <?php endforeach ?>
方法3:手动指定要显示的分类ID(无需改数据库)
如果不想调整数据库结构,可以直接在代码里定义允许显示的分类ID列表,循环时判断当前分类是否在列表中:
<?php // 定义要显示的分类ID列表,替换成你实际需要的ID $allowedCategoryIds = [1, 3, 5, 7]; foreach ($getCategories as $item): if(!in_array($item['category_id'], $allowedCategoryIds)) continue; ?> <div> <ul class="ul-nav ul-nav-default"> <li class="ul-nav-header"><a class="ul-text-secondary ul-text-capitalize" href="<?php echo $urlPath->search(['category' => $item['category_slug']]); ?>"><?php echo echoOutput($item['category_title']); ?></a></li> <?php $getSubCategories = getSubCategories($connect, $item['category_id']); $limitedSubCategories = array_slice($getSubCategories, 0, 3); foreach ($limitedSubCategories as $subItem): ?> <li><a href="<?php echo $urlPath->search(['subcategory' => $subItem['subcategory_slug']]); ?>"><?php echo echoOutput($subItem['subcategory_title']); ?></a></li> <?php endforeach ?> </ul> </div> <?php endforeach ?>
内容的提问来源于stack exchange,提问作者ale25uy
相关产品推荐
相关产品推荐

