Current Location: Home> Latest Articles> PHPCMS Guide to Getting Subcategory IDs

PHPCMS Guide to Getting Subcategory IDs

M66 2025-10-31

Methods to Retrieve Subcategory IDs in PHPCMS

Using the getCategoryChildIds Method

The getCategoryChildIds method takes a parent category ID as a parameter and returns all child category IDs under that parent. This method allows you to quickly obtain a collection of all subcategory IDs under the current category.

$categoryChildrenIds = getCategoryChildIds($parentId);

Using the getCategoriesByParent Method

The getCategoriesByParent method takes a parent category ID as a parameter and returns an array of categories containing all child categories under the parent. This method allows access to complete information about each subcategory, including their IDs.

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

Using the siteurl Function to Check for Subcategories

The siteurl function generates the URL for a category based on its ID. If the category does not exist, it returns a 404 error. This feature can be used to check whether a category has subcategories.

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

Example Usage

// Get the current category ID
$currentCatId = getCurrentCategory();

// Use getCategoryChildIds() to get subcategory IDs
$categoryChildrenIds = getCategoryChildIds($currentCatId);

// Use getCategoriesByParent() to get subcategory information
$categoryChildren = getCategoriesByParent($currentCatId);

// Use siteurl() to check if there are subcategories
$hasChildren = (siteurl($currentCatId) !== '404');