Category Images On Homepage - php

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

Related

magento all subcategories including name link and image

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>

Magento: Adding child categories to left nav menu

im using magento. This is the code that loads my left navigational bar. It loads the main categories into a ol. I want to also make it load the child categories within each li. Where I have put LOAD CHILD CATEGORIES HERE I would like to know the php code to fetch and display all the child categories of the above parent category. I have found several fixes but not knowing pHp very well has resulted in a series of errors.
/**
* Category left navigation
*
* #see Mage_Catalog_Block_Navigation
*/
if (!Mage::registry('current_category')) {
//no current category selected so default to showing the Products category
$cur_category=Mage::getModel('catalog/category')->load(51);
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
}else{
//current category selected so display the child categories for it
$layer = Mage::getSingleton('catalog/layer');
$_category = $layer->getCurrentCategory();
$currentCategoryId= $_category->getId();
if($currentCategoryId == 306){
//best sellers is selected so show the Product category
$cur_category=Mage::getModel('catalog/category')->load(51);
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
}
}
$_categories=$this->getCurrentChildCategories();
$_count = is_array($_categories)?count($_categories):$_categories->count();
if($_count):
?>
<div class="box layered-nav">
<div class="head">
<h3><?php echo $this->__('Browse By') ?> PRODUCT CATEGORY</h3>
</div>
<div class="border-creator">
<div class="narrow-by">
<dl id="narrow-by-list">
<dd>
<ol>
<?php foreach ($_categories as $_category): ?>
<?php if($_category->getIsActive()): ?>
<li>
<a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="active"<?php endif ?>><?php echo $this->htmlEscape($_category->getName()) ?></a>
**LOAD CHILD CATEGORIES IN HERE**
</li>
<?php endif; ?>
<?php endforeach ?>
</ol>
</dd>
</dl><script type="text/javascript">decorateDataList('narrow-by-list')</script>
</div>
</div>
</div>
<?php
endif;
?>
<!-- [ends] .browse-by // -->
So far the best I come up with, I know its not much
<ul class="subnav">
<li><a></a>
</ul>
I appreciate any help immensely
Check the following code
<?php $_helper = Mage::helper('catalog/category');
$_categories = $_helper->getStoreCategories();
$currentCategory = Mage::registry('current_category') ;
if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
<?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; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Now $_subcategories = $_category->getChildrenCategories() fetches all subcategories.
Thanks Chanz i used your code snippet and eventually came up with the following. Took a while cus im a little slow but you gave me what i needed. Much apreciated.
/**
* Category left navigation
*
* #see Mage_Catalog_Block_Navigation
*/
if (!Mage::registry('current_category')) {
//no current category selected so default to showing the Products category
$cur_category=Mage::getModel('catalog/category')->load(51);
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
}else{
//current category selected so display the child categories for it
$layer = Mage::getSingleton('catalog/layer');
$_category = $layer->getCurrentCategory();
$currentCategoryId= $_category->getId();
if($currentCategoryId == 306){
//best sellers is selected so show the Product category
$cur_category=Mage::getModel('catalog/category')->load(51);
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
}
}
$_categories=$this->getCurrentChildCategories();
$_count = is_array($_categories)?count($_categories):$_categories->count();
if($_count):
?>
<div class="box layered-nav">
<div class="head">
<h3><?php echo $this->__('') ?> PRODUCT CATEGORIES</h3>
</div>
<div class="border-creator">
<div class="narrow-by">
<dl id="narrow-by-list">
<dd>
<ol>
<?php foreach ($_categories as $_category): ?>
<?php if($_category->getIsActive()): ?>
<li>
<a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="active"<?php endif ?>><?php echo $this->htmlEscape($_category->getName()) ?></a>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<?php if($_category->getIsActive()): ?>
<li>
<a href="<?php echo $this->getCategoryUrl($_subcategory) ?>"<?php if ($this->isCategoryActive($_subcategory)): ?> class="active"<?php endif ?>><?php echo $this->htmlEscape($_subcategory->getName()) ?></a>
</a>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ol>
</dd>
</dl><script type="text/javascript">decorateDataList('narrow-by-list')</script>
</div>
</div>
</div>
<?php
endif;
?>
<!-- [ends] .browse-by // -->

How to get current category and its subcategories in magento?

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

Magento Mage::helper('catalog/category')

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

Magento category link to 404 error

this is my first Magento project and I've run into such a big issue.
The Magento version I'm using is 1.7.0.2
I managed to create a custom left bar navigation bar using the following code in my "app/design/frontend/default/default/template/catalog/navigation/category.phtml"
<?php
$cats = Mage::getModel('catalog/category')->load(1)->getChildren();
$catIds = explode(',',$cats);
?>
<div id="LeftCategory">
<h2>Wicked Categories</h2>
<hr style="color:white; height:1px; margin:5px;" />
<ul>
<?php foreach($catIds as $catId): ?>
<li class="Li-Top-Category">
<?php
$category = Mage::getModel('catalog/category')->load($catId);
?>
<a href="<?php echo $category->getUrl()?>">
<?php echo $category->getName()?>
</a>
<?php
$subCats = Mage::getModel('catalog/category')->load($category->getId())->getChildren();
$subCatIds = explode(',',$subCats);
?>
<?php if(count($subCatIds) > 1):?>
<ul>
<?php foreach($subCatIds as $subCat) :?>
<li class="Li-Sub-Category">
<?php
$subCategory = Mage::getModel('catalog/category')->load($subCat);
?>
<a href="<?php echo $subCategory->getUrl()?>">
<?php echo $subCategory->getName()?>
</a>
<?php
?>
</li>
<?php endforeach;?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
It does the work to generate the category navigation. But the issue is, the URL each link points to doesn't work. All links are invalid that lead to the 404 Error Page.
You can see it here: http://wicked-shop.dev.thejinstudio.com/shop/catalog/category/view/s/accessories/id/15/
I've done so much search and nothing really solved this issue.
I appreciate your help in advance.
Use the catalog/category helper to get the proper URL from a category model
$_catalogCatgoryHelper = Mage::helper('catalog/category');
Then with that helper pass your category to the getCategoryUrl() function
$_catalogCatgoryHelper->getCategoryUrl($category);
So give this a try. I've put my suggestions into your code:
<?php
$_catalogCatgoryHelper = Mage::helper('catalog/category');
$cats = Mage::getModel('catalog/category')->load(1)->getChildren();
$catIds = explode(',',$cats);
?>
<div id="LeftCategory">
<h2>Wicked Categories</h2>
<hr style="color:white; height:1px; margin:5px;" />
<ul>
<?php foreach($catIds as $catId): ?>
<li class="Li-Top-Category">
<?php
$category = Mage::getModel('catalog/category')->load($catId);
?>
<a href="<?php echo $_catalogCatgoryHelper->getCategoryUrl($category) ?>">
<?php echo $category->getName()?>
</a>
<?php
$subCats = Mage::getModel('catalog/category')->load($category->getId())->getChildren();
$subCatIds = explode(',',$subCats);
?>
<?php if(count($subCatIds) > 1):?>
<ul>
<?php foreach($subCatIds as $subCat) :?>
<li class="Li-Sub-Category">
<?php
$subCategory = Mage::getModel('catalog/category')->load($subCat);
?>
<a href="<?php echo $_catalogCatgoryHelper->getCategoryUrl($subCategory)?>">
<?php echo $subCategory->getName()?>
</a>
<?php
?>
</li>
<?php endforeach;?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
You might be better off with this code which I used in the past (but actually ended up rewriting so that it iteratively displays categories regardless of how deep they go, looking into how to do this would certainly be a good exercise for you). It is a bit cleaner, but you'll need to modify it slightly to your needs:
<?php $helper = $this->helper('catalog/category') ?>
<div class="block block-categorynavigation">
<div class="block-title">
<strong><span><?php echo $this->__('Category') ?></span></strong>
</div>
<div class="block-content">
<?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 $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 $subsubcategories = $subcategory->getChildren() ?>
<?php if (count($subcategories) > 0): ?>
<ul id="leftnav-tree-<?php echo $category->getId() ?>" class="level2">
<?php foreach($subsubcategories as $subsubcategory): ?>
<li class="level2<?php if ($this->isCategoryActive($subsubcategory)): ?> active<?php endif; ?>">
<?php echo $this->escapeHtml(trim($subsubcategory->getName(), '- ')) ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
edit: Original code only showed subcatgories for the currently viewed category

Categories