RobyBoy
RobyBoy

Reputation: 3

I want to get category and put in the page title in magento

this is the code that i want to modify(in 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() ?>

I want to get category and put in title.My code is:

?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() ?>

It works well when I'm in the category item but the home page does not open it anymore. I think $_title is empty or something like that. I tried to implement if statement but is the same problem.

Sorry for my english.

Upvotes: 0

Views: 1362

Answers (2)

ROBIN
ROBIN

Reputation: 502

You can Manage the meta details from the admin itself. Admin > Catalog > Manage Category. You don't need to modify any template file.

Manage the meta info

Mage::registry("current_category");

Above code returns the current category object. So on all the pages except the category ones, above registry will have no existence. So you will get error on all pages except the Category Pages.

Upvotes: 5

Please try this code.

<?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() ?>

Upvotes: 0

Related Questions