I'm building a Magento store and want to be able to display a list of categories and have each category link to its own page.
I have a 'Brands' category with an ID of 42 and I want to display a list of the sub-categories and ensure that each one links to the designated URL key in the CMS.
Has anyone had experience of doing this with Magento?
If you're comfortable editing your theme, this code snippet will bring you a list of all sub-categories of the current category (from the session, so this should work anywhere in your theme). I typically use this in app/design/frontend/default/theme_name/template/catalog/category/view.phtml
<?php
$_category = $this->getCurrentCategory();
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper = Mage::helper('catalog/category');
?>
<ul>
<?php foreach ($collection as $cat):?>
<?php if($_category->getIsActive()):?>
<?php
$cur_category = Mage::getModel('catalog/category')->load($cat->getId());
$_img = $cur_category->getImageUrl();
?>
<li>
<a href="<?php echo $helper->getCategoryUrl($cat);?>">
<img src="<?php echo $_img?>" title="<?php echo $cat->getName();?>"/>
<cite><?php echo $cat->getName();?></cite>
</a>
</li>
<?php endif?>
<?php endforeach;?>
</ul>
If You want to Display top level categories and subcategories U can do 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 $_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; ?>
For Displaying Top Level Categories and Current Categories SubCategories you can Do like ....
<?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; ?>
This question requires a long answer. I will point you to the right places.
1) Best solution is to use the free extension. I haven't tried it, but it will suit the purpose. You will have to do some CSS to achieve the right look and feel.
http://www.magentocommerce.com/extension/1562/magento-easy-catalog-images
Demo: http://extension01.templates-master.com/gb/electronics.html
2) I do not trust in modules as it might become difficult to upgrade if the vendor decided to stop supporting it. I have used the information from the following forum thread to create a vew sites. Have a look... Might not be straight forward. You might have to make some copies of core files to the local directory.
http://www.magentocommerce.com/boards/viewthread/3770/P30/
Hopefully this will be of help to you :)
I made this little video on how I create custom category listing blocks with Magento.
I am sure there are better ways of achieving this or even something I could have done better, but it’s just my method. I only created this it in hopes that it helps explain somethings to some people out there.
Magento Custom Category Listing Block
Thanks!
after looking at all the solutions on the magento site, i found that wookiehangover's solution above worked and took about 8 seconds to implement.
creates a UL that you can style. thanks.
After creating static block you can get any list of the subcategories by this script:
$_helper = Mage::helper('catalog/category');
$_category = Mage::getModel('catalog/category')->load(5);
$_subcategories = $_category->getChildrenCategories();
if (count($_subcategories) <= 0) { return; }
$count = 0;
foreach($_subcategories as $_category) {
$category = Mage::getModel('catalog/category')->load($_category->getId());
$ret->{"object_".$count} ->url = $_helper->getCategoryUrl($_category);
$ret->{"object_".$count} ->name = $_category->getName();
$ret->{"object_".$count} ->id = $_category->getId();
$ret->{"object_".$count} ->image = $category->getImageUrl();
$count++;
}
return $ret;
}
$list = list_subcategories(5);
echo "<pre>"; print_r($list); echo "</pre>";
?>
How about listing only the categories belonging to the current item. Not all the categories on the page.
But in a tree like view.
CATEGORIE - sub cat 1
CATEGORIE 2 - sub cat 1 - sub sub cat 1
BR Cveto
Related
So I'm using this code to display the categories that any product on my website is included in. The issue is that I'm using some categories as placeholders to show 'featured products' I wanted to try to filter these out.
This is Magento 1.9.1.0 on PHP 5.6.30
<ul class="listfix">
<?php $categories = $_product->getCategoryIds(); ?>
<?php foreach($categories as $k => $_category_id):
?>
<?php $_category= Mage::getModel('catalog/category')->load($_category_id)?>
<?php if($_category->getId()):?>
<li><a href="<?php echo $_category->getUrl() ?>"><?php echo $_category-
>getName() ?></a>
</li>
<?php endif;?>
<?php endforeach; ?>
</ul>
I tried adding this
$_category->addAttributeToFilter('is_active', 1)//get only active
categories
but it threw an error, I'm not a great php guy, I just find pieces of code and try to make them work. I got the original parts from HERE
as per below I've tried to add in the code that follows but I'm still seeing categories listed that are not active...
<ul class="listfix">
<?php $activeCategories =
Mage::getResourceModel('catalog/category_collection')
->addAttributeToFilter('is_active', 1)
->getColumnValues('entity_id');
?>
<?php $activeCategories = $_product->getCategoryIds(); ?>
<?php foreach($activeCategories as $k => $_category_id): ?>
<?php $_category= Mage::getModel('catalog/category')->load($_category_id)?>
<?php if($_category->getId()):?>
<li><a href="<?php echo $_category->getUrl() ?>"><?php echo $_category-
>getName() ?></a>
</li>
<?php endif;?>
This will give you a list of all the active Categor ID's in 1.9:
$activeCategories = Mage::getResourceModel('catalog/category_collection')
->addAttributeToFilter('is_active', 1)
->getColumnValues('entity_id');
Then you loop through that array, and load the category, or whatever, by ID:
foreach($activeCategories as $id) {
$cat = Mage::getModel('catalog/category')->load($id);
}
<ul class="listmem">
<?php $activeCategories =
Mage::getResourceModel('catalog/category_collection')
->addAttributeToFilter('is_active', 1)
->getColumnValues('entity_id');?>
<ul class="listfix">
<?php $activeCategories = $_product->getCategoryIds(); ?>
<?php foreach($activeCategories as $id) {
$cat = Mage::getModel('catalog/category')->load($id);
} ?>
<?php if($cat->getId()):?>
<li><a href="<?php echo $cat->getUrl() ?>"><?php echo $cat->getName() ?>
</a>
</li>
<?php endif;?>
</ul>
I thought I sorted out what I was doing wrong, but I'm still seeing the categories that aren't active in my list.
So I'm building a custom breadcrumb nav to list all fundraising options. The pages are using a unique layout called "fundraising_page." Is there a way to grab the pages only if they have the "fundraising_page" layout? So far I have this, which is grabbing every page regardless of the template it is using.
So what I need is only to list the pages that are using the "fundraising_page" template.
<?php $collection = Mage::getModel('cms/page')->getCollection()->addStoreFilter(Mage::app()->getStore()->getId());?>
<?php $collection->getSelect()->where('is_active = 1'); ?>
<ul>
<?php foreach ($collection as $page): ?>
<?php $PageData = $page->getData(); ?>
<?php if($PageData['identifier']!='no-route'){ ?>
<li>
<?php echo $PageData['title'] ?>
</li>
<?php } ?>
<?php endforeach; ?>
Here's some well formatted code and using proper methods.
<?php
$collection = Mage::getModel('cms/page')->getCollection()
->addStoreFilter(Mage::app()->getStore()->getId())
->addFieldToFilter('is_active', 1)
->addFieldToFilter('root_template', 'fundraising_page');
?>
<?php foreach ($collection as $page): ?>
<?php if ($page->getIdentifier() != 'no-route'): ?>
<li>
<?php echo $page->getTitle() ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
Instead of if($PageData['identifier']!='no-route') try
if($PageData['root_template']=='fundraising_page')
maybe it is something simple but i am not really good with php
With this code i am able to load the static blocks styled in top of category in Magento and the subcategories in the bottom when the top category has child
What i am trying to do is that when there is no child category it should only load the block
otherwise because of styling it will hold the place that the subcat had position.
Please have a look on my code and maybe you understand:
<?php
$_helper = $this->helper('catalog/output');
$_category = $this->getCurrentCategory();
$_imgHtml = '';
if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '<div class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /></div>';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
$_catHtml = $this->getChildHtml('catalog.inline.subcat');
?>
<?php if (!Mage::registry('current_category')) return ?>
<?php $_categories = $this->getCurrentChildCategories() ?>
<?php $_count = is_array($_categories)?count($_categories):$_categories->count(); ?>
<?php if($_count): ?>
// load the blocks styled with ul and il backgrounds
<ul class="inline-categories">
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId( $this->getCurrentCategory()->getLandingPage() )->toHtml() ?>
<?php foreach ($_categories as $_category): ?>
<li<?php if($_i == 1):?> class="first"<?php endif ?>><a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>>
<?php echo $this->htmlEscape($_category->getName()) ?></a> (<?php echo $_category->getProductCount() ?>)</li>
<?php endforeach ?>
<?php endif; ?>
</ul>
// here i need something like an "else" because the if($count) allready understand that this is not on count so should only load this that is without style //
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId( $this->getCurrentCategory()->getLandingPage() )->toHtml() ?>
//end of this else thing, now should only load the cms/block alone without anything / end of page we are done...
You can use PHP's if/else statements like this:
<?php if ($_count): ?>
stuff if $_count is non-zero
<?php else: ?>
other stuff if $_count is zero
<?php endif; ?>
Just to give you an idea. I hae 3 levels of categories.
Top Level > Sub Categories > Children of Sub Categories
What I don't want to display is the children of the sub categories.
The below code shows every category. (Sorry the paste didn't format very well.)
<?php
// ---- PRODUCT NAVIGATION ---- \\
$nav_obj = new Mage_Catalog_Block_Navigation();
$store_cats = $nav_obj->getStoreCategories();
$current_cat = $nav_obj->getCurrentCategory();
?>
<ul>
<?php foreach ($store_cats as $cat):?>
<?php
$ids = strtolower(str_replace(" ", "_", $cat->getName()));
$ids = str_replace("&", "", $ids);
?>
<li class="parent" id="<?php echo $ids; ?>"><?php echo $cat->getName()?>
<ul>
<?php foreach (Mage::getModel('catalog/category')->load($cat->getId())->getChildrenCategories() as $childCategory): ?>
<li><a href="<?php echo $childCategory->getUrl(); ?>">
<?php echo $childCategory->getName()?></a></li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
I've got a similar thing I'm working on. I'm basically passing over each category and rendering the stuff I want to render:
<?php # get category list ?>
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php foreach($_categories as $_category) : ?>
<?php # turn it into the proper model object ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php echo $_category->getName() ?>
<?php foreach($_category->getChildrenCategories() as $_subCategory) : ?>
<?php echo $_subCategory->getName() ?>
<?php endforeach ?>
<?php endforeach ?>
i want to check via php if a page is a cms_page in Magento. I need diffrent breadcrumbs for cms pages, so im trying to this with a condition, but i have no idea how to or where to look at.
Heres my breadcrumbs.phtml so far.
<?php if(this is a cms page): ?>
<p>some content</p>
<?php else: ?>
<?php if($crumbs && is_array($crumbs)): ?>
<div class="breadcrumbs">
<ul>
<?php $charsges = 0; ?>
<?php foreach($crumbs as $_crumbName=>$_crumbInfo): ?>
<?php
$charsges = strlen($_crumbInfo['label']) + $charsges;
if($charsges > 40){
$chars = 18;
if(strlen($_crumbInfo['label']) > $chars){
$_crumbInfo['label'] = substr($_crumbInfo['label'], 0, $chars);
$_crumbInfo['label'] = $_crumbInfo['label'].'..';
}
}
?>
<li class="<?php echo $_crumbName ?>">
<?php if($_crumbInfo['link']): ?>
<?php echo $this->htmlEscape($_crumbInfo['label']) ?>
<?php elseif($_crumbInfo['last']): ?>
<strong><?php echo $this->htmlEscape($_crumbInfo['label']) ?></strong>
<?php else: ?>
<?php echo $this->htmlEscape($_crumbInfo['label']) ?>
<?php endif; ?>
<?php if(!$_crumbInfo['last']): ?>
<span> > </span>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
greets rito
The following should give you what you want
//from a block or phtml script
$this->getRequest()->getModuleName()
When this returns the string 'cms', you're on a CMS page.
When Magento's frontend and admin routers can't find a match on your URL, the CMS router takes over. If the CMS router finds a match (based on the CMS pages you've setup), it hands off the request to the cms module and Mage_Cms_IndexController controller.