Looking to find a way to make a line break e.g <br />
on a product name.
3 Answers
Try to print out a product name after replacing the first whitespace character with <br />
:
$productName = $_helper->productAttribute($_product, $_product->getName(), 'name');
echo preg_replace('/\s{1}/u', '<br/>', $productName, 1);
Default template located here: app/design/frontend/base/default/template/catalog/product/list.phtml
Change at lines ~55 & ~96:
<h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a></h2>
to
<h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo preg_replace('/\s{1}/u', '<br/>', $_helper->productAttribute($_product, $_product->getName() , 'name'), 1); ?></a></h2>
I needed to do the same so I used str_replace.
I've added ' - ' (space dash space) in all the product titles I wanted my line break (You can use ' --- ' if you're using the ' - ' with other titles)
Then I've looked for
<?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?>
in list.phtml & list_featured_slider.phtml and I replaced it with
<?php
$productname = $_helper->productAttribute($_product, $_product->getName(), 'name');
$newproductname = str_replace(' - ', '<br />', $productname);
echo $newproductname
?>
Worked for me just fine ;)
You can do it in view.phtml as well if you need the line break on product page
Thanks to your hints, here my working solution to replace "-" with < br> in the product title (category view = app/design/frontend/base/default/template/catalog/product/list.phtml):
<strong class="product name product-item-name">
<a class="product-item-link" href="<?php echo $_product->getProductUrl() ?>">
<?php echo str_replace(' - ', '<br />', $_helper->productAttribute($_product, $_product->getName() , 'name')); ?></a>
</strong>
RESULT:
previous
POLO RALPH LAUREN Uomo - Giacca leggera antivento in nylon blu
now
POLO RALPH LAUREN Uomo
Giacca leggera antivento in nylon blu
Manufacture Model Awesomeness
and you want to do the following:Manufacture <br /> Model Awesomeness
on a product page?