如何在Concrete5.6的属性复选框列表中过滤mpdID为81的项?
解决Concrete5.6属性复选框过滤mpdID=81项的方案
嘿,我来帮你搞定这个过滤需求!在Concrete5.6里要去掉mpdID为81的复选框项,其实只需要在生成选项列表的时候加个简单的过滤逻辑就行,下面给你两种常见的实现方式:
方式一:提前过滤选项数组
如果你是先获取所有选项再循环输出,那可以先把不需要的项过滤掉再处理:
// 先获取你的属性对象(替换成实际的属性句柄) $attribute = AttributeKey::getByHandle('your_attribute_handle'); $allOptions = $attribute->getOptions(); // 过滤掉mpdID等于81的选项 $filteredOptions = array_filter($allOptions, function($option) { return $option->mpdID != 81; }); // 循环输出过滤后的复选框 foreach($filteredOptions as $option) { // 这里写你原来的复选框输出代码,比如: echo '<div class="checkbox-item"> <input type="checkbox" name="attribute_values[]" value="' . $option->mpdID . '" id="attr-' . $option->mpdID . '"> <label for="attr-' . $option->mpdID . '">' . htmlspecialchars($option->value) . '</label> </div>'; }
方式二:循环时跳过指定项
如果不想额外处理数组,也可以在循环输出的时候直接跳过mpdID=81的项:
$attribute = AttributeKey::getByHandle('your_attribute_handle'); $options = $attribute->getOptions(); foreach($options as $option) { // 遇到mpdID为81的项直接跳过 if ($option->mpdID == 81) { continue; } // 输出复选框代码 echo '<div class="checkbox-item"> <input type="checkbox" name="attribute_values[]" value="' . $option->mpdID . '" id="attr-' . $option->mpdID . '"> <label for="attr-' . $option->mpdID . '">' . htmlspecialchars($option->value) . '</label> </div>'; }
两种方式都能达到目的,你可以根据自己的代码结构选更顺手的那个。注意替换代码里的your_attribute_handle为你实际使用的属性句柄,还有输出部分要和你原代码的HTML结构保持一致哦。
内容的提问来源于stack exchange,提问作者Justyn
相关产品推荐
相关产品推荐

