How to Dynamic generate Open Graph protocol meta tags
-
22-10-2019 - |
문제
<meta property="og:url" content="<?php echo "http://". $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];?>"/>
<meta property="og:description" content=""/>
<meta property="og:title" content=""/>
<meta property="og:image" content=""/>
I've tested with the head.phtml
file,but I don't know how to get product attributes such like sku
, color
and images
.
I tested with $this->helper('catalog/image')->init($_product, 'image')
found in
media.phtml
on meta tag and get a blank page,but images on product view are working fine.
That confuses me a lot.
해결책
You need to can add this to one of your layout files:
<catalog_product_view>
<reference name="head">
<block type="core/template" name="open-graph" as="open-graph" template="opengraph/product.phtml" />
</reference>
</catalog_product_view>
Now you need to create the file opengraph/product.phtml
inside you theme (app/design/frontend/{package}/{theme}/template/
) with the following content:
<?php $_product = Mage::registry('current_product'); ?>
<?php if ($_product) : ?>
<meta property="og:url" content="<?php echo "http://". $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];?>"/>
<meta property="og:description" content="<?php echo htmlspecialchars($_product->getDescription())?>"/>
<meta property="og:title" content="<?php echo htmlspecialchars($_product->getName())?>"/>
<meta property="og:image" content="<?php echo $this->helper('catalog/image')->init($_product, 'image')?>"/>
<?php endif;?>
If you need to include the sku
or color
in there you can get them like this:
$sku = $_product->getSku();
$color = $_product->getAttributeText('color');
Clear the cache when you are done.
제휴하지 않습니다 magento.stackexchange