0

I have a custom attribute for my product page. This attribute allows me to display 'badges' such as free deliver, etc. The attribute value is the name of the badge image and I use the below code in the temporal to display the images. However, there is always a trailing forward slash at the end of the URL so the images don't display. How can I get rid of this? Thank you in Advance

Andy

Code in the template:

    <div class="product-attribute product-attribute--badges">
        <ul class="product-attribute__list">
            <?php foreach ($badges as $badge) : ?>
                <?php $img_src = strtolower(preg_replace('/[^A-Za-z0-9]/', '', $badge)); ?>
                <li class="product-attribute__item">
                    <img
                        class="product-attribute__image"
                        src="<?php echo $this->getUrl('pub/media/wysiwyg/badge/' . $img_src . '.png') ?>"                       
                        alt="<?php echo $this->escapeHtml($badge); ?>"
                        title="<?php echo $this->escapeHtml($badge); ?>"
                    />
                </li>
            <?php endforeach; // $badges as $badge ?>
        </ul>
    </div>
<?php endif; // !empty($badges) && count($badges) > 0 ?>```

1
  • Thanks Guys - I ended up using the below which di the trick: src="<?php echo $this->getUrl('', array('_direct'=>'pub/media/wysiwyg/badge/' . $img_src . '.png')) ?>"
    – AndyJH
    Jun 11, 2021 at 11:25

2 Answers 2

1

use below code instead of your code

    <div class="product-attribute product-attribute--badges">
        <ul class="product-attribute__list">
            <?php foreach ($badges as $badge) : ?>
                <?php $img_src = strtolower(preg_replace('/[^A-Za-z0-9]/', '', $badge)); ?>
                <li class="product-attribute__item">
                    <img
                        class="product-attribute__image"
                        src="<?php echo $this->getUrl().'pub/media/wysiwyg/badge/'. $img_src.'.png'; ?>"                       
                        alt="<?php echo $this->escapeHtml($badge); ?>"
                        title="<?php echo $this->escapeHtml($badge); ?>"
                    />
                </li>
            <?php endforeach; // $badges as $badge ?>
        </ul>
    </div>
<?php endif; // !empty($badges) && count($badges) > 0 ?>
1
  • Accept solution if works for you:) Jun 5, 2021 at 8:13
0

I suggest you try this:

src="<?php echo $this->getUrl().'pub/media/wysiwyg/badge/'. $img_src'.png' ?>"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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