Showing results for 
Search instead for 
Did you mean: 

Use of str_replace in .phtml File

Use of str_replace in .phtml File

In My Magento 2 Home Page, If I insert Widgets in content, then my product image is not frontend

Path Navigation from Admin: Content -> Pages -> select the Home Page -> Edit -> Content -> Insert Widget of Widget Type Catalog New Products list


After inserting Widget, my content is updated with the below code:

{{widget type="Magento\Catalog\Block\Product\Widget\NewWidget" display_type="all_products" show_pager="0" products_count="10" template="product/widget/new/content/new_grid.phtml"}}


But if I call those product images by creating anther Widget of Widget Type Catalog New Products list and specifying them in CMS Home Page in Design Layout, my product images are displaying properly.


I found the reason behind this that when I am inserting Widget in page content , my image path is not being set properly


My Correct Image Path URL under root directory



My Wrong Image Path URL under root director


Issue is %5C should have to be transformed into \ i.e. URL encoding is not happening when I am inserting Widget in page content 

So, I want to use str_replace to replace %5C to \ forcefully but yet not successful.

I am using Magento Community Edition, Magento ver. 2.4.3-p1


Please suggest me how to resolve such issue 




Re: Use of str_replace in .phtml File

Inside a custom module, it is a little bit harder to change templates. Here is an example of changing topmenu.html coming from the module Magento_Theme. In your module the template needs to be placed here:


Additionally, a layout definition is required:



<?xml version="1.0"?>
<page layout="1columns" xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<referenceBlock class="Magento\Theme\Block\Html\Topmenu" name="catalog.topnav" template="${VENDORNAME}_${MODULENAME}::html/topmenu.phtml" ttl="false"/>

Have a look at app/code/Magento/Theme/view/frontend/layout/default.xml for reference, where this XML crap can be found

If issue solved, Click Kudos & Accept as Solution.
LitCommerce - The Most Simple & Affordable Multi-channel Selling Tool

Re: Use of str_replace in .phtml File

Hi Expert,

I am using Magento default blank theme.

The relative path for default.xml from root directory is 


Under default.xml my area of concern is

<container name="content" label="Main Content Area"/> => this is line no 112

I want to replace widget image path so that my images can be visible. 

I am using module-catalog.


Please suggest.



Re: Use of str_replace in .phtml File

hi anik, were you able to solve this issue?