Reputation: 3273
Is there a way to display a category image by it's category id in a CMS page?
You can display a category link by id with:
{{widget type="catalog/category_widget_link" anchor_text="Displayed Text" title="Title attribute text" template="catalog/category/widget/link/link_block.phtml" id_path="category/22"}}
Or display category products by it's id.
{{block type="catalog/product_list" category_id="14" template="catalog/product/list.phtml"}}
But I can't figure out hwo to display a specific categories image in a CMS page just by calling it's id.
Any idea?
Upvotes: 2
Views: 9293
Reputation: 10114
In stock magento you cant.
There are essentially 3 ways of achieving this, in order of preference (good to bad imo):
1. Use a widget
I have previously answered a similar question and provided a complete example on how to build such a widget specific to a category:
magento - category name and image in static block?
2. Custom module
You nned to create a module with a block to support this. You can then just include the block in cms pages using the following syntax:
{{block type="yourmodule/category_image" category_id="14" template="yourmodule/category/image.phtml"}}
Your block class is going to look like this:
<?php
class Yourcompany_Yourmodule_Block_Category_Image extends Mage_Core_Block_Template
{
public function getCategory()
{
if (! $this->hasData('category')) {
$category = Mage::getModel('catalog/category')->load($this->getData('category_id'));
$this->setData('category', $category);
}
return $this->getData('category');
}
}
and your template class is going to look like this:
<?php
$_helper = $this->helper('catalog/output');
$_category = $this->getCategory();
$_imgHtml = '';
if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '<p class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /></p>';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
?>
<?php if($_imgUrl): ?>
<?php echo $_imgHtml ?>
<?php endif; ?>
3. Use core/template block type
Include on the cms page like so..
{{block type="core/template" category_id="14" template="category/image.phtml"}}
Create your template - catalog/category/image.phtml
<?php
$_helper = $this->helper('catalog/output');
$category = Mage::getModel('catalog/category')->load($this->getData('category_id'));
$_imgHtml = '';
if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '<p class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /></p>';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
?>
<?php if($_imgUrl): ?>
<?php echo $_imgHtml ?>
<?php endif; ?>
Upvotes: 3
Reputation: 2790
I don't think you'll be able to just call the image because it is not a block. In /design/base/default/template/category/view.phtml, it's just calling the image url as an attribute of the block and then building the output HTML right in the template file. There isn't any specific element in the block that renders it to call with magento's shortcode.
Best best would be to build a new widget. There's plenty of guides, and they're pretty awesome once you get to know them better.
Upvotes: 1