我想获得类别并在magento中放入页面标题

时间:2017-05-16 13:54:59

标签: magento title

这是我要修改的代码(在head.phtml中):

<meta http-equiv="Content-Type" content="<?php echo $this->getContentType() ?>" />
<title><?php echo $this->getTitle() ?></title>
<meta name="description" content="<?php echo htmlspecialchars($this->getDescription()) ?>" />
<meta name="keywords" content="<?php echo htmlspecialchars($this->getKeywords()) ?>" />
<meta name="robots" content="<?php echo htmlspecialchars($this->getRobots()) ?>" />
<link rel="shortcut icon" href="<?php echo $this->getFaviconFile(); ?>" type="image/x-icon" />
<?php echo $this->getCssJsHtml() ?>
<?php echo $this->getChildHtml() ?>
<?php echo $this->getIncludes() ?>

我想获得类别并输入标题。我的代码是:

?php
$currentCategory = Mage::registry("current_category");
?>
<?php $_title = $currentCategory->getName(); ?>
<meta http-equiv="Content-Type" content="<?php echo $this->getContentType() ?>" />
<title><?php echo $_title ?></title>
<meta name="description" content="<?php echo htmlspecialchars($this->getDescription()) ?>" />
<meta name="keywords" content="<?php echo htmlspecialchars($this->getKeywords()) ?>" />
<meta name="robots" content="<?php echo htmlspecialchars($this->getRobots()) ?>" />
link rel="shortcut icon" href="<?php echo $this->getFaviconFile(); ?>" type="image/x-icon" />
<?php echo $this->getCssJsHtml() ?>
<?php echo $this->getChildHtml() ?>
<?php echo $this->getIncludes() ?>

当我在类别项目中但主页不再打开它时效果很好。 我认为$ _title是空的或类似的东西。我试图实现if语句但是同样的问题。

抱歉我的英文。

2 个答案:

答案 0 :(得分:5)

您可以从管理员本身管理元详细信息。管理员&gt;目录&gt;管理类别。您不需要修改任何模板文件。

Manage the meta info

Mage::registry("current_category");

上面的代码返回当前的类别对象。因此,在除类别之外的所有页面上,上述注册表将不存在。因此,除了类别页面之外,您将在所有页面上收到错误。

答案 1 :(得分:0)

请尝试此代码。

<?php if (Mage::registry('current_category')) : ?>
<?php $currentCategory = Mage::registry("current_category"); ?>
<?php $_title = $currentCategory->getName(); ?>
<?php else : ?>
<?php $_title = $this->getTitle(); ?>
<?php endif; ?>


<meta http-equiv="Content-Type" content="<?php echo $this- >getContentType() ?>" />
<title><?php echo $_title ?></title>
<meta name="description" content="<?php echo htmlspecialchars($this->getDescription()) ?>" />
<meta name="keywords" content="<?php echo htmlspecialchars($this->getKeywords()) ?>" />
<meta name="robots" content="<?php echo htmlspecialchars($this->getRobots()) ?>" />
<link rel="shortcut icon" href="<?php echo $this->getFaviconFile(); ?>" type="image/x-icon" />
<?php echo $this->getCssJsHtml() ?>
<?php echo $this->getChildHtml() ?>
<?php echo $this->getIncludes() ?>