當前位置: 首頁> 最新文章列表> PHPCMS 獲取下級欄目ID 方法詳解

PHPCMS 獲取下級欄目ID 方法詳解

M66 2025-10-31

在PHPCMS 中獲取下級欄目ID 的方法

使用getCategoryChildIds 方法

getCategoryChildIds 方法接受一個父欄目ID 作為參數,並返回該父欄目下所有子欄目的ID。使用此方法可以快速獲取當前欄目下所有下級欄目的ID 集合。

 $categoryChildrenIds = getCategoryChildIds($parentId);

使用getCategoriesByParent 方法

getCategoriesByParent 方法接受一個父欄目ID 作為參數,並返回一個欄目數組,其中包含該父欄目下的所有子欄目。使用此方法可以獲取子欄目的完整信息,包括它們的ID。

 $categoryChildren = getCategoriesByParent($parentId);
foreach ($categoryChildren as $category) {
    $categoryId = $category['catid'];
}

使用siteurl 函數判斷下級欄目

siteurl 函數可以根據欄目ID 生成該欄目的URL。如果訪問不存在的欄目,則返回404 錯誤,因此可以用此方法判斷欄目是否存在下級欄目。

 $exists = (siteurl($categoryId) !== '404');

示例操作

// 獲取當前欄目 ID
$currentCatId = getCurrentCategory();

// 使用 getCategoryChildIds() 方法獲取下級欄目 ID
$categoryChildrenIds = getCategoryChildIds($currentCatId);

// 使用 getCategoriesByParent() 方法獲取下級欄目信息
$categoryChildren = getCategoriesByParent($currentCatId);

// 使用 siteurl() 函數判斷是否有下級欄目
$hasChildren = (siteurl($currentCatId) !== '404');