Magento - Get category name in a custom product grid in homepage

Magento - Get category name in a custom product grid in homepage

August 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Get category name in a custom product grid in homepage

Displaying Category Names in Custom Product Grids on Magento Homepage

As a senior developer and technical blogger, I often receive queries about customizing product grids on the Magento homepage. One common question is how to display category names alongside products in these grids.

Magento’s built-in grid systems don’t directly support displaying category names. However, with some creative use of Magento’s block system and layout XML, you can achieve this functionality.

Understanding the Requirements

In your case, you want to show a custom product list on the homepage, with each product row including its corresponding category name. This requires:

  1. Creating a new product collection.
  2. Overriding or extending the default homepage layout.
  3. Inserting the custom block containing the product grid and category names.

Step 1: Create a New Product Collection

The first step is to create a new product collection that filters products based on your desired criteria. You can do this using Magento’s Mage::getModel() function:

$categoryId = 3; // Replace with your category ID

$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addCategoryFilter($categoryId)
    ->setPageSize(10) // Set the number of products to display
    ->setCurPage(1); // Set the current page (starting from 1)

foreach ($collection as $product) {
    echo $product->getName() . "<br>"; // Display product name
}

This code creates a new product collection filtered by category ID, sets the number of products to display, and iterates through each product in the collection.

Step 2: Override or Extend the Default Homepage Layout

To display your custom product grid on the homepage, you need to override or extend the default homepage layout XML. You can do this using a Magento extension or by directly modifying the page.xml file located in app/design/frontend/base/default/layout.

Create a new layout update file named homepage_custom_grid.xml and place it in your theme’s layout folder (e.g., app/design/frontend/packagename/theme/layout). The contents of this file should look something like this:

<layout version="0.1.0">
    <default>
        <reference name="content">
            <block type="core/text_list" name="custom.homepage.grid" before="-">
                <action method="setTemplate">
                    <template>yourtheme/custom_homepage_grid.phtml</template>
                </action>
            </block>
        </reference>
    </default>
</layout>

This layout update configures a new core/text_list block named “custom.homepage.grid” and assigns it to the content area of your homepage. The before attribute specifies that this block should be placed before any existing blocks in the content area.

Step 3: Insert the Custom Block with Product Grid

The final step is to create a PHP template file (e.g., custom_homepage_grid.phtml) under your theme’s template directory (app/design/frontend/packagename/theme/template). In this file, you can display your custom product grid and category names using Magento’s product collection:

<?php
$categoryId = 3; // Replace with your category ID

$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addCategoryFilter($categoryId)
    ->setPageSize(10) // Set the number of products to display
    ->setCurPage(1); // Set the current page (starting from 1)

foreach ($collection as $product) {
    echo "<div class='custom-product'>";
        echo "Product: " . $product->getName() . "<br>";
        echo "Category: " . $product->getCategory()->getName();
    echo "</div>";
}
?>

This code creates a product collection just like the previous example and iterates through each product. For each product, it displays both the product name and its corresponding category name using the getCategory() method.

Conclusion

Displaying category names alongside products in custom grids on Magento’s homepage requires a combination of creating new product collections, overriding or extending layout XML files, and inserting a custom block with PHP template code. By following the steps outlined above, you can achieve this functionality while keeping your Magento installation clean and maintainable.

Remember to replace the placeholder category ID (3) with your actual category ID in all examples.

If you’re looking for professional Magento development services or need help with advanced Magento customizations, feel free to reach out to LaravelCompany for expert assistance.

Catalog Themes Admin

Our network