我使用的是magento 1.9。在产品页面上,元标题就像
“产品名称类别子类别”
我只想显示“产品名称 - Mysite.com”
如何仅在产品页面上进行此修复?
谢谢。
最佳答案
Magento 在两个不同的地方设置标题标签。首先在 Mage_Catalog_Block_Breadcrumbs 的 _prepareLayout() 方法中
$title = array();
$path = Mage::helper('catalog')->getBreadcrumbPath();
foreach ($path as $name => $breadcrumb) {
$breadcrumbsBlock->addCrumb($name, $breadcrumb);
$title[] = $breadcrumb['label'];
}
if ($headBlock = $this->getLayout()->getBlock('head')) {
$headBlock->setTitle(join($this->getTitleSeparator(), array_reverse($title)));
}
$path 变量的内容将根据您到达产品页面的方式而有所不同。例如:
产品直接链接:
- 网址:/aviator-sunglasses.html
- 标题:飞行员太阳镜
使用类别菜单导航至产品:
- 网址:/accessories/eyewear/aviator-sunglasses.html
- 标题:飞行员太阳镜 - 眼镜 - 配饰
然后在 Mage_Catalog_Block_Product_View 的 _prepareLayout() 方法中
$product = $this->getProduct();
$title = $product->getMetaTitle();
if ($title) {
$headBlock->setTitle($title);
}
这里它只是检查是否设置了元标题产品属性,如果设置了则覆盖之前设置的标题。因此,您有两个选择:
- 为每个产品设置元标题
重写Mage_Catalog_Block_Breadcrumbs类中的_prepareLayout()方法
if ($headBlock = $this->getLayout()->getBlock('head')) { if ($product = Mage::registry('current_product')) { $storeName = Mage::getStoreConfig('general/store_information/name'); $pageTitle = $storeName ? $product->getName() . ' - ' . $storeName : $product->getName(); $headBlock->setTitle($pageTitle); } else { $headBlock->setTitle(join($this->getTitleSeparator(), array_reverse($title))); } }
关于magento - 自定义 Magento 元标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27625825/