問題描述
我想將 2 個自定義屬性添加到產品頁面上的
標簽中.它們是品牌"和字幕".
I have 2 custom attributes I'd like to add to the <title>
tags on product pages. They are 'brand' and 'subtitle'.
我的頁面標題最終會是這樣的:
My page title would end up something like this:
$brand." ".$productname." ".$subtitle;
$brand." ".$productname." ".$subtitle;
我怎樣才能做到這一點?
How can I achieve this?
非常感謝您的幫助.
推薦答案
根據您的問題,我假設您指的是更改產品的元標題.
From your question, I assume you are referring to changing the meta title for products.
有 3 個選項可供您選擇:
There are 3 options open to you:
- 瀏覽每個產品并手動更新(或使用電子表格并單獨導入)每個產品元標題.這些值是編輯產品時可在管理區域中使用.
- 重寫 Mage_Catalog_Block_Product_View 并覆蓋_prepareLayout() 方法,即生成此標簽的位置.
- 使用觀察者并掛鉤到 catalog_controller_product_view 事件.
您的決定實際上是在選項 2 和選項 2 之間3(這兩者都需要你創建一個自定義模塊來實現).
Your decision is really between options 2 & 3 (both of which will require you to create a custom module to achieve).
在擴展 Magento 核心功能時,我總是盡量不引人注目 - 所以我會在這里選擇選項 3.請參閱以下代碼以獲取完整示例:
I always try to be as unobtrusive as possible when extending Magento core functionality - so I would opt for option 3 here. Please see below code for a complete example:
app/etc/modules/Yourcompany_Yourmodule.xml
app/etc/modules/Yourcompany_Yourmodule.xml
<?xml version="1.0"?>
<config>
<modules>
<Yourcompany_Yourmodule>
<active>true</active>
<codePool>local</codePool>
</Yourcompany_Yourmodule>
</modules>
</config>
app/code/local/Yourcompany/Yourmodule/etc/config.xml
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>
<yourmodule>
<class>Yourcompany_Yourmodule_Model</class>
</yourmodule>
</models>
</global>
<frontend>
<events>
<catalog_controller_product_view>
<observers>
<yourmodule>
<class>Yourcompany_Yourmodule_Model_Observer</class>
<method>catalog_controller_product_view</method>
</yourmodule>
</observers>
</catalog_controller_product_view>
</events>
</frontend>
</config>
app/code/local/Yourcompany/Yourmodule/Model/Observer.php
app/code/local/Yourcompany/Yourmodule/Model/Observer.php
<?php
class Yourcompany_Yourmodule_Model_Observer
{
/**
* Change product meta title on product view
*
* @pram Varien_Event_Observer $observer
* @return Yourcompany_Yourmodule_Model_Observer
*/
public function catalog_controller_product_view(Varien_Event_Observer $observer)
{
if ($product = $observer->getEvent()->getProduct()) {
$title = $product->getData('brand') . ' ' . $product->getData('name') . ' ' . $product->getData('sub_title');
$product->setMetaTitle($title);
}
return $this;
}
}
這篇關于Magento 更改產品頁面標題以包含屬性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!