Magento - Show / hide CMS block when display mode 'Static Block and Products' selected in category

Magento - Show / hide CMS block when display mode 'Static Block and Products' selected in category

October 6, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Show / hide CMS block when display mode 'Static Block and Products' selected in category

Magento: Conditionally Show / Hide CMS Blocks Based on Layered Navigation

In Magento, displaying or hiding static blocks based on layered navigation is a common requirement for enhancing user experience. This guide will walk you through the process of showcasing static blocks only when no layered navigation options are selected while displaying products otherwise.

Understanding the Requirement

The goal here is to create a dynamic layout that adapts to the user’s interaction with the layered navigation filters. When none of the filters (brand, price range, etc.) are applied, we want the toys.phtml static block to appear alongside the product list. Conversely, if any filter is selected (like brand=1), we aim to hide this static block and display only the filtered products.

The Approach

To achieve this behavior, we’ll employ a combination of Magento’s built-in functionality along with custom JavaScript for real-time updates.

1. Creating a Custom Template

First, ensure you have created a custom template file in your theme that extends Magento_Catalog/templates/category/product/list.phtml. This will allow us to override the default product list rendering process without modifying core files (which is against Magento best practices).

2. Conditionally Displaying Static Blocks

Within this custom template file, we can use PHP conditionals to check whether any layered navigation filters are active and accordingly display or hide the static block.

<?php if (!$this->getLayer()->getState()->getFilterProcessed()) : ?>
    <?= $blockHtml = $block->toHtml() ?>
<?php endif; ?>

<!-- Your product list code here -->
<div class="products wrapper-grid">
    <!-- Product items -->
</div>

In this example, $this->getLayer()->getState()->getFilterProcessed() checks if any filters have been processed. If not (! denotes negation), we display the static block using <?= $blockHtml = $block->toHtml() ?>. Otherwise, it remains hidden.

3. Implementing Real-time Updates with JavaScript

To provide a seamless user experience, we need to update this display/hide behavior in real-time as users interact with the layered navigation filters. For this purpose, you can utilize Magento’s built-in require function to load custom JavaScript files and handle filter events accordingly.

4. Updating the Static Block

Ensure that your static block (toys.phtml) is assigned to the category view page layout using the Magento admin interface or XML layout updates.

Conclusion

By following these steps, you can effectively create a dynamic category page in Magento that adapts its content based on user interaction with layered navigation filters. This approach ensures both performance and usability while maintaining a clean codebase that adheres to Magento’s best practices.

Remember, implementing such customizations often requires a solid understanding of Magento’s core functionality and can benefit from using tools like Laravel Company’s Magento Extension Builder to streamline the development process.

Catalog Themes Pricing JavaScript

Our network