magento - 动态获取 magento 元关键字?

标签 magento seo meta

在magento的产品页面上,我想在meta keywords标签中获取产品名称、类别名称和子类别名称。

请帮忙!!

提前致谢。

最佳答案

由于产品已经具有附加的 MetaKeyword 值,您可以使用观察者以不显眼的方式扩展该值。该方法不涉及扩展核心类

试试这个:

/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);
        }
    }

}

关于magento - 动态获取 magento 元关键字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7606564/

相关文章:

php - 我怎样才能在 PHP 中找到所有内容,直到找到其他内容并将其作为字符串返回?

angular 2 location.go vs window.location.href

php - window.location (JS) vs header() (PHP) 用于重定向

magento - 上传特定文件时出现 SSL 错误 : invalid or self signed certificate in Magento 1. 9

zend-framework - 将 Magento 与 CRM 软件集成

php - 布局 xml 中的 Magento 条件 block

magento - 如何在magento中为每个产品设置相关产品的代码

duplicates - 我如何将所有要复制的流量或搜索(或类似的 URL)定向到我们网站上的一个 URL?

php - html php 元字符集 UTF-8 不工作?

android - 使屏幕的窗口宽度