Remove Line Breaks With A Magento Attribute

December 15, 2015
Posted in Magento
December 15, 2015 MItch Daniel

Remove Line Breaks With A Magento Attribute

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

Nothing special just telling css to not display

Fix 2: Removing nl2br from short descriptions in Magento

Short Description

Locate the view.phtml file

Open your view.html in your favorite text editor and look for

Before:

Make the following changes, simply remove the nl2br

After:

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.

Magento Attribute

Step 2.

Add it to your attribute set. I added mine under the short description. Once complete click Save.

Magento Attribute

Step 3.

Locate the view.phtml file

Open your view.html in your favorite text editor and look for

Before:

After:

Step 4: Upload saved files to the server

 

Leave a Reply

Your email address will not be published. Required fields are marked *