1 回答

TA貢獻1824條經驗 獲得超8個贊
最簡單的方法是將已經顯示的類別存儲在 an 中,Array并使用in_array檢查您要顯示的類別是否已經在其中
編輯:存儲 id 可能比存儲名稱更好,因為如果已經獲取,您可以避免獲取名稱:
<?php
$diplayed_categories = []; //initializing array
foreach ($_productCollection as $_product):
?>
<div class="bk-all-products">
<?php
$bk_product_id = $_product->getCategoryIds();
$bk_category_id = $bk_product_id[1];
if(!in_array($bk_category_id, $diplayed_categories)){ //testing if not in array
$diplayed_categories[] = $bk_category_id; //filling the array
//moved inside the if, no need to fetch it again if it exists
//$categoryId = $bk_category_id; //useless var
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $_objectManager->create('Magento\Catalog\Model\Category')
->load($bk_category_id); //replaced by $bk_category_id
$bk_category_id_name = $category->getName();
echo $bk_category_id_name;
echo "<br><br>";
}
?>
</div>
<?php endforeach; ?>
- 1 回答
- 0 關注
- 286 瀏覽
添加回答
舉報