magento show tab if content exists

magento show tab if content exists

September 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
magento show tab if content exists

How to Show Magento Product Tabs Only When Content Exists

Hello developers! In this post, we’ll explore how to show Magento product tabs only when there is content available for that specific tab. This is particularly useful for custom attributes where you don’t want an empty tab to appear.

Understanding the Current Setup

The existing code provided uses jQuery and jQuery UI to create product tabs directly linked from modern theme’s tab.phtml file. The XML configuration in catalog.xml adds a new tab called “How to Use” which links to how-to-use.phtml block.

<action method="addTab" translate="title" module="catalog">
    <alias>how-to-use</alias>
    <title>How to Use</title>
    <block>catalog/product_view</block>
    <template>catalog/product/view/how-to-use.phtml</template>
</action>

The tab.phtml file has a structure that loops through the available tabs and displays them:

<div id="tabs">
  <!-- Tab contents -->
  <?php foreach ($this->getTabs() as $_index => $_tab): ?>
    <?php if($this->getChildHtml($_tab['alias'])): ?>
      <li class="<?php echo !$_index?' active first':(($_index==count($this->getTabs())-1)?' last':'')?>"><a href="#<?php echo $_tab['alias'] ?>"><?php echo $_tab['title']?></a></li>
    <?php endif; ?>
  <?php endforeach; ?>
</div>

The how-to-use.phtml file checks if the custom attribute “How to Use” has content before displaying it:

<?php $_howtouse = $this->getProduct()->getHowToUse(); ?>
<?php if ($_howtouse): ?>
  <div class="std">
    <?php echo $this->helper('catalog/output')->productAttribute($this->getProduct(), $_howtouse, 'howtouse') ?>
  </div>
<?php endif; ?>

The Challenge: Detecting Empty Tabs

The current setup successfully displays the “How to Use” tab when content is available. However, the question arises about how to replicate this behavior for other custom tabs where we want to avoid blank tabs.

We need a way to check if there’s actually something to display before adding the tab to the UI.

The Solution: Checking Tab Content Before Display

To achieve this, we should update our tab.phtml file to include a conditional check for each tab. Instead of always displaying the tab regardless of content availability, we’ll only add it when it has actual data.

Let’s revise the loop in tab.phtml:

<?php foreach ($this->getTabs() as $_index => $_tab): ?>
  <?php $childHtml = $this->getChildHtml($_tab['alias']); ?>
  <?php if (!empty($childHtml) || (is_string($childHtml) && trim($childHtml))): ?>
    <li class="<?php echo !$_index?' active first':(($_index==count($this->getTabs())-1)?' last':'')?>"><a href="#<?php echo $_tab['alias'] ?>"><?php echo $_tab['title']?></a></li>
  <?php endif; ?>
    <div class="product-tabs-content" id="<?php echo $_tab['alias'] ?>" style="<?php if (empty($childHtml) || (is_string($childHtml) && trim($childHtml))): echo 'display:none;'; endif; ?>"><?php echo $this->getChildHtml($_tab['alias']) ?></div>
<?php endforeach; ?>

In this code, we first fetch the child HTML for each tab. Then, before displaying the tab and its content block:

  • We check if $childHtml is not empty.
  • If it’s a string, we also check if trimming whitespace leaves any non-empty characters.

This double-check ensures that even hidden whitespace or empty strings don’t trigger an otherwise unnecessary tab display.

Conclusion

By incorporating this logic into the tab.phtml file, you can now create custom product tabs that only appear when there is actual content to display. This maintains a clean and user-friendly interface by avoiding empty sections.

Remember, you’ll need to apply similar checks for any new custom attributes or blocks you add as product tabs.

For more Magento development tips and best practices, feel free to explore our other posts at Laravel Company.

Happy coding!

Catalog Indexing Database Extensions

Our network