currently i have a subcategory page that shows the correct names, pulls the right links and displays everything in an orderly maner.
However, i have come upon a peculiar problem, as the images are all the same picture pulled from one of the categories. this should of course be the image related to the category name, can anyone spot where i have made a mistake?
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $categoryId = 465;?>
<?php $category = Mage::getModel('catalog/category')->load($categoryId) ?>
<?php $_categories = $category->getChildrenCategories() ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php if($_category->hasChildren()):?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<ul class="catblocks">
<?php foreach($_subcategories as $_subcategory): ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<?php echo $_subcategory->getName() ?>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"><img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getImage() ?>" alt="<?php echo $_subcategory->getName() ?>" />
<span><?php echo $_subcategory->getName() ?></span></a>
<?php $_category2 = Mage::getModel('catalog/category')->load($_subcategory->getId()) ?>
<?php if($_category2->hasChildren()):?>
<?php $_subcategories2 = $_category2->getChildrenCategories() ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
any help is apreciated :)
<?php
//gets all sub categories of parent category
$cats = Mage::getModel('catalog/category')->load(6)->getChildren();
$catIds = explode(',',$cats);
$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = array(
'url' => $category->getUrl(),
'img' => $category->getImageUrl()
);
}
ksort($categories, SORT_STRING);
?>
<ul>
<?php foreach($categories as $name => $data): ?>
<li>
<a href="<?php echo $data['url']; ?>" title="<?php echo $name; ?>">
<img class="cat-image" src="<?php echo $data['img']; ?>" />
</a>
</li>
<?php endforeach; ?>
</ul>
Related
I have a piece of custom code for my magento solution that displays images and text in a static block element, allowing me to have a visual representation of a category view.
However even though the code should sort my categories according to their position in the admin panel this does not happen.
im sitting on a magento 1.9.x.x solution and this is my code hoping someone has some insight here.
<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'image'))
->addAttributeToFilter('is_active', 1)
->addAttributeToFilter('include_in_menu', 1)//if not to show in nav
->addIdFilter($category->getChildren())
->addAttributeToSort(‘position’,'ASC');
?>
<?php $_columnCount = 4; ?>
<?php if ($i++%$_columnCount==0): ?>
<ul class="catblocks">
<?php foreach ($categories as $category): ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first <?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<a href="<?php echo $category->getUrl() ?>">
<img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getImage() ?>"
alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
<span><?php echo $category->getName() ?></span>
</a>
<?php $i++ ?>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
In your code addAttributeToSort(‘position’,'ASC') is using non-ASCII quotes around 'position' and the ending </ul> should come before endif; since the opening tag comes after the start of the if statement.
<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'image'))
->addAttributeToFilter('is_active', 1)
->addAttributeToFilter('include_in_menu', 1)//if not to show in nav
->addIdFilter($category->getChildren())
->addAttributeToSort('position','ASC');
?>
<?php $_columnCount = 4; ?>
<?php if ($i++%$_columnCount==0): ?>
<ul class="catblocks">
<?php foreach ($categories as $category): ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first <?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<a href="<?php echo $category->getUrl() ?>">
<img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getImage() ?>"
alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
<span><?php echo $category->getName() ?></span>
</a>
<?php $i++ ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
I have a file that is successfully looping through my categories and subcategories. I am successfully able to echo all the categories and their links
BUT
I do not understand why these (keywords and description) are not echoing
<?php echo htmlspecialchars($this->getKeywords()) ?>
<?php echo htmlspecialchars($this->getDescription()) ?>
The file is located here
app/design/frontend/mystoretheme/default/template/catalog/category/listofcats.phtml
Then im placing it on a block in a cms page {{block type="catalog/navigation" name="catalog.category" template="catalog/category/listofcats.phtml"}}
The goal is to be able to display each of the categories list of keywords and their descriptions within the same < li > and loop giving me a list like this
Category
Keywords
Description
Here is my code. I have omitted my attempts at keyword and description since they are not working.
<div class="block block-list block-categories">
<div id="block-categories" class="block-title active">
<strong><span>Categories </span></strong>
</div>
<div id="leftnav" class="block-content" style="display:block">
<?php $helper = $this->helper('catalog/category') ?>
<?php $categories = $this->getStoreCategories() ?>
<?php if (count($categories) > 0): ?>
<ul id="leftnav-tree" class="level0">
<?php foreach($categories as $category): ?>
<li class="level0<?php if ($this->isCategoryActive($category)): ?> active<?php endif; ?>">
<span><?php echo $this->escapeHtml($category->getName()) ?></span>
<?php //if ($this->isCategoryActive($category)): ?>
<?php $subcategories = $category->getChildren() ?>
<?php if (count($subcategories) > 0): ?>
<ul id="leftnav-tree-<?php echo $category->getId() ?>" class="level1">
<?php foreach($subcategories as $subcategory): ?>
<li class="level1<?php if ($this->isCategoryActive($subcategory)): ?> active<?php endif; ?>">
<?php echo $this->escapeHtml(trim($subcategory->getName(), '- ')) ?>
<?php $secondLevelSubcategories = $subcategory->getChildren() ?>
<?php if (count($secondLevelSubcategories ) > 0): ?>
<ul id="leftnav-tree-<?php echo $subcategory->getId() ?>" class="level2">
<?php foreach($secondLevelSubcategories as $secondLevelSubcategory ): ?>
<li class="level2<?php if ($this->isCategoryActive($secondLevelSubcategory )): ?> active<?php endif; ?>">
<?php echo $this->escapeHtml(trim($secondLevelSubcategory ->getName(), '- ')) ?>
</li>
<?php endforeach; ?>
</ul>
<script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
<?php endif; ?>
<?php //endif; ?>
</li>
<?php endforeach; ?>
</ul>
<script type="text/javascript">decorateList('leftnav-tree', 'recursive')</script>
<?php endif; ?>
</div>
Please check this:
<?php
$categories = Mage::getModel('catalog/category')
->getCollection()
->setStoreId(Put the store id here)
->addAttributeToSelect('*')
->addIsActiveFilter();
foreach ($categories as $category) {
echo $category->getName();
echo "-";
echo $category->getMetaKeywords();
echo "-";
echo $category->getMetaDescription();
echo "<br/>";
}
?>
I know I can iterate over categories to get ids of product and load them in the view, but I would have liked to get a product collection as it is done currently in most categories/views.
in short how to get subcategory.
To get current category and its sub categories, you can try like this
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach ($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
<?php if ($currentCategory && $currentCategory->getId() == $_category->getId()): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach ($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
You can follow this tutorial
I've been using this to get the category list:
Mage::helper('catalog/category')->getStoreCategories()
But i have a problem. It only show the categories that have "YES" on Include in Navigation Menu *.
Here is the whole code.
<?php if(Mage::helper('dynamicsitemap')->showCategories()): ?>
<div class="sitempan">
<h2 class="smh2">Our Categories</h2>
<?php $_helper = Mage::helper('catalog/category') ?>
<?php echo $_helper->getStoreCategories() ?>
<?php
$_categories = Mage::helper('catalog/category')->getStoreCategories() ?>
<?php if (count($_categories) > 0): ?>
<ul class="sitecatul">
<?php foreach($_categories as $_category): ?>
<?php //echo $_category->isEnabled(); ?>
<li class="cat">
<strong class="strongsm"><?php echo $_category->getName() ?></strong>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<?php $_nextcategory = Mage::getModel('catalog/category')->load($_subcategory->getId()) ?>
<?php $_nextsubcategories = $_nextcategory->getChildrenCategories() ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>" title="<?php echo $_subcategory->getName() ?>">
<?php echo $_subcategory->getName() ?>
</a>
<?php if (count($_nextsubcategories) > 0): ?>
<ul>
<?php foreach($_nextsubcategories as $_nextsubcat): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_nextsubcat) ?>" title="<?php echo $_subcategory->getName() ?> - <?php echo $_nextsubcat->getName() ?>">
<?php echo $_nextsubcat->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
I want to know what can i do to show all categories, even they are not listed in the menu, to use it as a sitetree.
Try and change line 7 to:
$_categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*');
Try changing
$_categories = Mage::helper('catalog/category')->getStoreCategories();
to
$_categories = $categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*')->addIsActiveFilter();
I am using the code below which pulls in all of my SUB-categories from my main product category on to the homepage.
I need to somehow be able to pull in the category images of the subcategories as well under the getName function. I have tried a few methods mentioned before but none of them have worked.
i am using 1.7.0 if that helps.
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()); ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li class="item">
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Managed to achieve it by changing the code to below
<?php
//gets all sub categories of parent category 'Brands'
$cats = Mage::getModel('catalog/category')->load(27)->getChildren();
$catIds = explode(',',$cats);
$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = array(
'url' => $category->getUrl(),
'img' => $category->getImageUrl()
);
}
ksort($categories, SORT_STRING);
?>
<?php foreach($categories as $name => $data): ?>
<li class="item">
<a href="<?php echo $data['url']; ?>" title="<?php echo $name; ?>">
<div style="font:12px/15px Arial,Helvetica,sans-serif; margin-bottom:10px;"><?php echo $name; ?></div>
<img class="cat-image" src="<?php echo $data['img']; ?>" />
</a>
</li>
<?php endforeach; ?>
you need to write something like this under getName function
<img src="<?php echo $_subcategory->getImageUrl()">