1

I am successfully using this code to display a brand logo

php $brand=$_product->getAttributeText('brand');

echo '

'_',$brand).'.jpg" alt="'.$brand.'">'

now what i need help with is making it a link as well

the problem is where as

My brands have capital letters as my attribute does so for example Brand Punch It Performance uses image Punch_It_performance.jpg

however the url for it is

/shop-by-brand/punch-it-performance.html

now forgive my newbieness to php here but i could really use some help modifying the above code snippet to function so when its clicked it goes to the proper page

1
  • Wouldnt let me post this code above <?php $brand=$_product->getAttributeText('brand'); echo '<img src="/skin/frontend/default/default/images/media/'.str_replace(' ', '_',$brand).'.jpg" alt="'.$brand.'">' ?> </div> Oct 3, 2016 at 19:51

1 Answer 1

1

This should do it:

<?php
$brand = $_product->getAttributeText('brand');
$brandImage = str_replace(' ', '_', $brand) . ".jpg";
$brandLink = strtolower(str_replace(' ', '-', $brand)) . ".html";
$brandBaseUrl = Mage::getBaseUrl() . '/shop-by-brand/';
?>
<a href="<?php echo $brandBaseUrl . $brandLink; ?>" title="<?php echo $brand; ?>">
    <img src="<?php echo $this->getSkinUrl("images/media/" . $brandImage); ?>" alt="<?php echo $brand; ?>" />
</a>

Edit1: fixed typo

8
  • getting a syntax error on this line Oct 3, 2016 at 20:13
  • $brandLink = strtolower(str_replace(' ', '-', $brand) . ".html"; Oct 3, 2016 at 20:14
  • I got this working <?php $brand=$_product->getAttributeText('brand'); echo strtolower('<a href="/'.str_replace(' ', '-',$brand).'.html" alt="'.$brand.'">') ; echo '<img src="/skin/frontend/default/default/images/media/'.str_replace(' ', '_',$brand).'.jpg" alt="'.$brand.'">' ?></a> Oct 3, 2016 at 20:26
  • I updated my answer, that line was missing a character. Oct 4, 2016 at 5:54
  • awesome thanks , now say i have another attribute "restrictions" with 2 options Off Road and Race Only i tried using the same code snippet to display a image named Off_Road.jpg and Race_Only.jpg but it come back blank maybe i didnt set up the new attribute correctly? Oct 4, 2016 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.