I’ll show you how I removed line breaks from short descriptions with a Magento attribute You might be wondering why your product description or short description is all spaced out, that’s because magento or your theme is adding automatic line breaks into the content description area.
This issue could be caused by your theme, 3rd party modules, or older versions of Magento. I’ve seen a few different fixes, like remove nl2br from template files or add a css trick to hide the <br/> but nothing that really suited my needs.
In this article I will show you how I added a magento attribute to turn on and off nl2br which adds line breaks to your short description. You might find it useful at times to have it on at other times to have it off. I will be focusing on the short description. But keep in mind this can be applied to short descriptions and full descriptions in your view.phtml, list.phtml and description.html or any files that call your descriptions or short descriptions.
Lets get started. Ill first show you some of the fixes I came across.
Quick Notes I’m using Magento 1.9.1.1 and a custom theme. This will work with Magento 1.8.X upwards.
Fix 1: Use CSS to hide <br/> in short descriptions
1 2 3 |
.short-description .std > br { display: none !important; } |
Nothing special just telling css to not display
Fix 2: Removing nl2br from short descriptions in Magento
Short Description
Locate the view.phtml file
1 |
/app/design/frontend/[package]/[theme]/template/catalog/product/view.phtml |
Open your view.html in your favorite text editor and look for
Before:
1 2 3 4 5 |
<?php if ($_product->getShortDescription()):?> <div class="short-description"> <div class="std"><?php echo $_helper->productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?></div> </div> <?php endif;?> |
Make the following changes, simply remove the nl2br
After:
1 2 3 4 5 |
<?php if ($_product->getShortDescription()):?> <div class="short-description"> <div class="std"><?php echo $_helper->productAttribute($_product, ($_product->getShortDescription()), 'short_description') ?></div> </div> <?php endif;?> |
This will disable the automatic <br/> line breaks from your short description.
But what if you want both? Then I have the solution for you. It’s easy as creating a Magento Attribute
When Magento attributes Short Descriptions HTML set to Yes I am able to keep the existing <br/> in my products short description, and it would filter out the nl2br that was causing doubles <br/>for me.
When Magento attributes Short Descriptions HTML set to No short description I would not have to manually add <br/>. In this case, Magento would add nl2br and give me the line breaks.
Step 1.
Create a Magento attribute with the following settings. Take note of your attribute code.
Step 2.
Add it to your attribute set. I added mine under the short description. Once complete click Save.
Step 3.
Locate the view.phtml file
1 |
/app/design/frontend/[package]/[theme]/template/catalog/product/view.phtml |
Open your view.html in your favorite text editor and look for
Before:
1 2 3 4 5 |
<?php if ($_product->getShortDescription()):?> <div class="short-description"> <div class="std"><?php echo $_helper->productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?></div> </div> <?php endif;?> |
After:
1 2 3 4 5 6 7 8 9 10 11 |
<?php if ($_product->getShortDescription()):?> <div class="short-description"> <div class="std"> <?php if ($_product->getAttributeText('is_short_des_html') == Yes) { echo $_helper->productAttribute($_product, ($_product->getShortDescription()), 'short_description');} else { echo $_helper->productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description'); } ?> </div> </div> <?php endif;?> |
Step 4: Upload saved files to the server