Showing custom static block on the custom page Magento

Showing custom static block on the custom page Magento

September 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Showing custom static block on the custom page Magento

Displaying Custom Static Blocks on Magento

Hello developers! We often face situations where we need to display specific blocks of content on designated pages within our Magento stores. In this post, I’ll guide you through displaying custom static blocks based on the current category or page.

Understanding the Problem

Let’s say you have three static blocks: Block 1, Block 2, and Block 3. You want to display:

  • Block 1 on Category 1 and its subcategories
  • Block 2 on Category 2 and its subcategories
  • Block 3 on all other pages (home, about, etc.)

However, when you use Mage::app()->getFrontController()->getRequest()->getRequestUri(), the request looks like “category1.html”. If a user navigates to a subcategory of this category, the block changes back to default.

If you use Mage::app()->getFrontController()->getRequest(), the output is more complex: “catalog/category/view/id/id_number”.

Solving the Issue

To solve this issue, we need to identify the current page type and then check which category it belongs to. Here’s a step-by-step approach:

  1. Identify the Page Type

    First, let’s determine if the current page is a category or not. We can use the getRequest() method to get the path from the request.

$request = Mage::app()->getFrontController()->getRequest();
$currentPath = $request->getPathInfo();
  1. Check for Category Pages

    Now, we need to check if this path is a category page or not. A category page will have “catalog/category/view” in the path.

if (strpos($currentPath, 'catalog/category/view') !== false) {
    // This is a category page
} else {
    // This is not a category page
}
  1. Identify the Category

    If it’s a category page, we can extract the category ID from the path:

$categoryId = intval(str_replace('catalog/category/view/id/', '', $currentPath));
  1. Display the Correct Block

    Now that you have the category ID (or know it’s not a category), you can display the correct static block using the getLayout()->getBlock('cms/block')->setBlockId($blockId); method.

Here is an example of how to set up this code in your controller:

public function indexAction()
{
    $request = Mage::app()->getFrontController()->getRequest();
    $currentPath = $request->getPathInfo();

    if (strpos($currentPath, 'catalog/category/view') !== false) {
        $categoryId = intval(str_replace('catalog/category/view/id/', '', $currentPath));

        // Check which category it is
        // For example, you have Block 1 for Category 1
        if ($categoryId == '1') {
            Mage::app()->getLayout()->getBlock('cms/block')->setBlockId('block_1');
        }
    } else {
        // This is not a category page, so display the default block or no block at all
        Mage::app()->getLayout()->getBlock('cms/block')->setBlockId('');
    }

    $this->loadLayout();
    $this->renderLayout();
}

Conclusion

Displaying static blocks based on the current page in Magento can be a bit tricky, but with the right approach, it’s quite straightforward. By identifying the page type and then checking for specific categories or paths, you can display your blocks exactly where you need them.

Remember to replace ‘block_1’, ‘block_2’, and ‘block_3’ with the actual IDs of your static blocks.

For more advanced customizations in Magento, I recommend visiting Laravel Company, which provides comprehensive guides and services for Magento development.

Catalog Errors

Our network