I'm working on a Magento site which is using the Fishpig Wordpress Extension. We have the Categories widget displaying in the left sidebar & it is set to show hierarchy.
It is working two levels deep (i.e. ul & li's with .level0 & .level1), but is not showing categories 3 levels deep i.e. level2
I've tested this on a basic wordpress installation and I can get it to display categories 3 levels down but I can't get it to work on Magento with the fishpig WordPress integration. I have assigned posts to all the sub categories.
I see in template/wordpress/sidebar/widget/categories.phtml that there is this code block to get the level1 child categories:
<?php else: ?>
<?php foreach($categories as $category): ?>
<li class="level0 item<?php if ($this->isCurrentCategory($category)): ?> active<?php endif; ?>">
<a class="level0" href="<?php echo $category->getUrl() ?>" title="<?php echo $category->getName() ?>">
<?php echo $category->getName() ?>
</a><?php if ($this->getCount()): ?> (<?php echo $category->getPostCount() ?>)<?php endif; ?>
<?php if ($this->getHierarchical()): ?>
<?php $children = $children = $category->getChildrenCategories() ?>
<?php if (count($children) > 0): ?>
<ul class="level1">
<?php foreach($children as $child): ?>
<?php if ($child->getPostCount() > 0): ?>
<li class="level1 item<?php if ($this->isCurrentCategory($child)): ?> active<?php endif; ?>">
» <?php echo $child->getName() ?><?php if ($this->getCount()): ?> (<?php echo $child->getPostCount() ?>)<?php endif; ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
<?php endif; ?>
Is there a way to display more than two levels of wordpress categories on Magento with Fishpig?
I updated template/wordpress/sidebar/widget/categories.phtml to include a 3rd level and it worked :)
<?php foreach($categories as $category): ?>
<li class="level0 item<?php if ($this->isCurrentCategory($category)): ?> active<?php endif; ?>">
<a class="level0" href="<?php echo $category->getUrl() ?>" title="<?php echo $category->getName() ?>">
<?php echo $category->getName() ?>
</a><?php if ($this->getCount()): ?> (<?php echo $category->getPostCount() ?>)<?php endif; ?>
<?php if ($this->getHierarchical()): ?>
<?php $children = $children = $category->getChildrenCategories() ?>
<?php if (count($children) > 0): ?>
<ul class="level1">
<?php foreach($children as $child): ?>
<?php if ($child->getPostCount() > 0): ?>
<li class="level1 item<?php if ($this->isCurrentCategory($child)): ?> active<?php endif; ?>">
» <?php echo $child->getName() ?><?php if ($this->getCount()): ?> (<?php echo $child->getPostCount() ?>)<?php endif; ?>
<?php $children2 = $children2 = $child->getChildrenCategories() ?>
<?php if (count($children2) > 0): ?>
<ul class="level2">
<?php foreach($children2 as $child2): ?>
<?php if ($child2->getPostCount() > 0): ?>
<li class="level12 item<?php if ($this->isCurrentCategory($child2)): ?> active<?php endif; ?>">
» <?php echo $child2->getName() ?><?php if ($this->getCount()): ?> (<?php echo $child2->getPostCount() ?>)<?php endif; ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
Related
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 have Magento with fishpig integration and Wordpress blog.
I can see magento products easy with this shortcode [product sku="skunumber"].
But I have a lot of trouble with categories, how can I display a Magento Category In WordPress article?
There is a shortcode, who can help me to do this Or do I need to code Term Collections?
Try these methods
To display all the Magento categories.
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
To Display Categories and Subcategories.
<?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 More Details Visit DISPLAY CATEGORIES AND 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
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();
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