Sub category listing in Magento - php

I've looked around a lot and found lots about pulling in parent Id's and sub cat listings but this is slight different and I don't seem to able to find an answer, I'm not a PHP guru (yet)
so go easy on me. I've tried various things but I just end up with errors.
I have created a phtml template to display sub categories of a parent category in an inline link block below my main nav. I'm calling this template with a static block from the admin which is working fine, but the link block disappears when I navigate through to a subcategory page, obviously because this code calls sub cats of a parent but not while you are actually IN a sub cat. here's the code I'm using so far:
<?php if (!Mage::registry('current_category')) return ?>
<?php $_categories = $this->getCurrentChildCategories() ?>
<?php $_count = is_array($_categories)?count($_categories):$_categories->count(); ?>
<?php if($_count): ?>
<div class="category-products <?php echo Mage::getModel('catalog/layer')->getCurrentCategory()->getName(); ?>">
<dl id="narrow-by-list2">
<dt></dt>
<dd>
<ol class="subcat_list">
<?php foreach ($_categories as $_category): ?>
<?php if($_category->getIsActive()): ?>
<li>
<a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>><?php echo $this->htmlEscape($_category->getName()) ?></a>
</li>
<?php endif; ?>
<?php endforeach ?>
</ol>
</dd>
</dl>
<script type="text/javascript">decorateDataList('narrow-by-list2')</script>
</div>
<?php endif; ?>
any ideas as to how I can modify this so the list stays there while I'm actually viewing a sub cat?
Many thanks

After much looking around I finally stumbled on what I needed
Thanks MagikSwapna for your input it helped me understand things a little more
I eventually ended up with this
<div class="category-products <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('parent_cat_name')->toHtml() ?>">
<?php echo "<ol class='subcat_list'>"; ?>
<?php
$currentCat = Mage::registry('current_category');
if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
{
// current category is a toplevel category
$loadCategory = $currentCat;
}
else
{
// current category is a sub-(or subsub-, etc...)category of a toplevel category
// load the parent category of the current category
$loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
// #TODO enhance for more nested category levels to display sub-categories
}
$subCategories = explode(',', $loadCategory->getChildren());
foreach ( $subCategories as $subCategoryId )
{
$cat = Mage::getModel('catalog/category')->load($subCategoryId);
if($cat->getIsActive())
{
if($crcat == $cat->getName()) //Check if current category is this subcategory
echo '<li><b>'.$cat->getName().'</b>'.'</li>'; //If yes display it as bold (Currently Selected)
else //
echo '<li>'.$cat->getName().''.'</li>'; //Otherwise display it as normal
}
}
?>
<?php echo "</ol>"; ?>
</div>
and it's working fine, I created a custom layout to use for the category pages and it will throw an error if used in a non-category related page, but it works!
Finally.

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<?php echo $_category->getName() ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<?php foreach($_subcategories as $_subcategory): ?>
<h3><?php echo $_subcategory->getName() ?></h3>
<!--sub sub category-->
<?php $_subcategory = Mage::getModel('catalog/category')->load($_subcategory->getId()) ?>
<?php $_subsubcategories = $_subcategory->getChildrenCategories() ?>
<?php if (count($_subsubcategories) > 0): ?>
<ul>
<?php foreach($_subsubcategories as $_subsubcategory): ?>
<li>
<?php echo $_subsubcategory->getName() ?>
<?php $_subsubsubcategory = Mage::getModel('catalog/category')->load($_subsubcategory->getId()) ?>
<?php $_subsubsubcategories = $_subsubcategory->getChildrenCategories() ?>
<?php if (count($_subsubsubcategories) > 0): ?>
<ul>
<?php foreach($_subsubsubcategories as $_subsubsubcategory): ?>
<li>
<span><?php echo $_subsubsubcategory->getName() ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<!--sub sub category-->
<?php endforeach; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

Related

Get most recent post on each category Magento Fishpig

I have a successful Fishpig integration to magento 2.
Now I need to get the blog page to display most recent post on each category.
Ex: If I have 5 categories (without showing uncategorized category), I need to display most recent post from each category.
How can I do this with fishpig?
I tried modifying list.phtml file,
from FishPig/WordPress/view/frontend/templates/post. So far I can only get the most recent posts from any category (if I have most recent posts in one category, I get them. not most recent post from each category).
This is the code I've got so far..
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance() ?>
<?php $posts = $objectManager->create('FishPig\WordPress\Model\ResourceModel\Post\Collection')
->addPostTypeFilter('post')
->setOrderByPostDate()
->addIsViewableFilter()
->setPageSize(5)
->load();
?>
<?php if (count($posts) > 0): ?>
<ul>
<?php foreach($posts as $post): ?>
<li>
<?php echo $this->escapeHtml($post->getPostTitle()) ?>
<?php if ($image = $post->getFeaturedImage()): ?>
<a href="<?php echo $post->getUrl() ?>">
<img src="<?php echo $image->getAvailableImage() ?>" src="<?php echo $this->escapeHtml($post->getPostTitle()) ?>" />
</a>
<?php endif; ?>
<p><?php echo $post->getPostExcerpt(40) ?></p></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
How should I modify this to achieve what I need? Please help
What is it that you want? The module, by default, will show you each category page and list all posts in that category in descending date order. Are you trying to replicate this?
If so, take a look at the Term model. You can load a collection of categories and then call getPostCollection() on it to get posts from that Term/category.
<?php $terms = $objectManager->get('FishPig\WordPress\Model\ResourceModel\Term\CollectionFactory')->create() ?>
<?php $terms->addTaxonomyFilter('category')->load() ?>
<?php if (count($terms) > 0): ?>
<ul>
<?php foreach($terms as $term): ?>
<?php if ((int)$term->getId() === 1): ?><?php /* This is the uncategorized category, so skip */ continue; ?><?php endif; ?>
<?php $posts = $term->getPostCollection()->setPageSize(5)->setOrderByPostDate()->addIsViewableFilter()->load() ?>
<?php if (count($posts) > 0): ?>
<li>
<h2><?php echo $term->getName() ?></h2>
<ul>
<?php foreach($posts as $post): ?>
<li>
<?php /* Standard post code here */
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>

Loop Through Magento Categories, Meta Keywords and Descriptions

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/>";
}
?>

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 // -->

Where is this title coming from

I am using magento and i have this code in my left_nav.phtml
<?php $_helper = Mage::helper('catalog/category');?>
<?php $_categories = $_helper->getStoreCategories();?>
<!-- BEGIN SIDE-NAV-CATEGORY -->
<div class="side-nav-categories">
<div class="block-title">
<?php echo ('Categories');?>
<?php var_dump(get_class($this)); ?>
</div><!--block-title-->
<?php if (count($_categories) > 0): ?>
<!-- BEGIN BOX-CATEGORY -->
<div class="box-content box-category">
<ul>
<!-- Subcategory -->
<?php
//get the current category
$_cat = new Mage_Catalog_Block_Navigation();
$currentCat = $_cat->getCurrentCategory();
//get the children of the current category
$subCats = Mage::getModel('catalog/category')->load($currentCat->getId())->getChildren() ;
//get sub category ids
$subCatIds = explode(',',$subCats);
?>
<?php if (count($subCatIds) > 1): ?>
<ul>
<?php foreach($subCatIds as $subCatId): ?>
<?php $subCat = Mage::getModel('catalog/category')->load($subCatId); ?>
<?php if($subCat->getIsActive()): ?>
<li>
<?php //display category image
if($subCat->getImageUrl()): ?>
<img src="<?php echo $subCat->getImageUrl(); ?>" />
<?php endif; ?>
<a href="<?php echo $subCat->getUrl(); ?>">
<?php echo $subCat->getName(); ?>
</a>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<!-- Subcategory-->
</ul>
</div><!--box-content box-category-->
<?php endif; ?>
</div><!--side-nav-categories-->
in the div class block-title it changes the title to search vehicle by category.
where can i find the code that defines block-title? so i can change the title to the root category name?

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

Categories