Magento2 Media path in .phtml file

Mitul picture Mitul · Feb 29, 2016 · Viewed 28.5k times · Source

I have tried lots of option in magento2 to find the media path URL

How can i find the media path URL in template.

I have try

$object_manager = Magento\Core\Model\ObjectManager::getInstance();
$dir = $object_manager->get('Magento\App\Dir');
$mediaUrl = $dir->getDir(\Magento\App\Dir::MEDIA);

But it give me error class not found. I have search and found the one solution to create function in block file but there are so many places i want to use the media path in my design

Thanks in advance.

Answer

1984 picture 1984 · Sep 1, 2016

This is how you get the media path from inside a block or template.

$this->getUrl('pub/media')