Get magento meta keywords dynamically?

2019-03-22 08:12发布

On the Product page of magento, i want to get the product name, its category name and sub category name in the meta keywords tag.

Please help!!

Thanks in advance.

标签: magento seo meta
3条回答
Summer. ? 凉城
2楼-- · 2019-03-22 08:28

Since products already have an attached MetaKeyword value, you can use an observer to unobtrusively extend that value. This method doesn't involve extending a core class

Try this:

/app/code/local/YourCompany/YourModule/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <YourCompany_YourModule>
            <version>1.0.0</version>
        </YourCompany_YourModule>
    </modules>
    <global>
        <models>
            <YourCompany_YourModule>
                <class>YourCompany_YourModule_Model</class>
            </YourCompany_YourModule>
        </models>
    </global>
    <frontend>
        <events>
            <catalog_controller_product_view>
                <observers>
                    <YourCompany_YourModule>
                        <class>YourCompany_YourModule/Observer</class>
                        <method>productView</method>
                    </YourCompany_YourModule>
                </observers>
            </catalog_controller_product_view>
        </events>
    </frontend>
</config>

/app/code/local/YourCompany/YourModule/Model/Observer.php

<?php
class YourCompany_YourModule_Model_Observer
{

    public function productView(Varien_Event_Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();
        /* @var $product Mage_Catalog_Model_Product */

        if ($product) {
            $keywords = $product->getMetaKeyword();

            // Add the product name
            $keywords = ' ' . $product->getName();

            // Add the category name
            $currentCategory = Mage::registry('current_category');
            if ($currentCategory && $currentCategory instanceof Mage_Catalog_Model_Category) {
                $keywords = ' ' . $currentCategory->getName();
            }

            $product->setMetaKeyword($keywords);
        }
    }

}
查看更多
趁早两清
3楼-- · 2019-03-22 08:28

Are you meaning product details page ? if yes means very simple

Goto your specific product page,there should be tab "Meta Information".here you can add

Meta Title, Meta Keywords, Meta Description
查看更多
萌系小妹纸
4楼-- · 2019-03-22 08:46

You have to rewrite the Mage_Catalog_Block_Product_View class, especially the __preparelayout() method.

Simply add the following code in the _prepareLayout method you will overwrite:

protected function _prepareLayout()
{
    $currentCategory = Mage::registry('current_category'); // For accessing current category information
    $product = $this->getProduct();

    if ($headBlock = $this->getLayout()->getBlock('head')) {
      $headBlock->setTitle("Whatever you want here");   
      $product->setMetaKeyword("whatever, keywords, you, want, here");   
      $product->setMetaDescription("Whatever description you want here);   
    }

    return parent::_prepareLayout();
}

It's important that you set the metakeyword and metadescription the way it's described above, else it will be overwritten again by the parent class(es).

Regards, Kenny

查看更多
登录 后发表回答