If you need to get some important URL in .phtml file of magento 2 then you can use this.
(1.) How to get URL of home page?
Example:
1 2 |
a. http://domain.com/ :- <?= $this→getUrl() ?> b. http://domain.com/contacts/ :- <?= $this→getUrl(‘contacts’) ?> |
(2.) How to get URL of static CSS/JS files
Example:
1 2 3 4 5 6 7 |
a. app/design/frontend/ThemeVendor/theme_name/web/css/custom.css <?= $this->getViewFileUrl('css/custom.css') ?> b. app/design/frontend/ThemeVendor/theme_name/Magefan_Blog/web/js/lazyload.js <?= $this->getViewFileUrl('Magefan_Blog::js/lazyload.js') ?> |
(3.) How to get URL of static pub/media or media URL()
Example:
1 2 3 |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $mediaUrl = $objectManager->get(Magento\Store\Model\StoreManagerInterface::class) ->getStore() →getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); |
(4.) How to get some important URL in the WYSIWYG editor
If you need to get some important URL in WYSIWYG editor of magento 2 then you can use this
(i) How to get URL of home page:
Example:
1 2 |
a. http://domain.com/ :- {{store url=’’}} b. http://domain.com/contacts/ :- {{store url=’contacts’}} |
(ii) How to get URL of static CSS/JS files:
Example:
1 2 3 4 5 6 7 |
a. app/design/frontend/ThemeVendor/theme_name/web/css/custom.css {{view url='css/custom.css'}} b. app/design/frontend/ThemeVendor/theme_name/Magefan_Blog/web/js/lazyload.js {{view url='Magefan_Blog::js/lazyload.js'}} |
(iii) How to get URL of static pub/media or mediaUrl():
Example:
1 |
{{media url="wysiwyg/image.png"}} |
bluethinkinc_blog
2022-09-19