Home > Magento Tips > Retrieve Categories / Subcategories

Retrieve Categories / Subcategories

Posted on: 22nd Dec 2010 By: Robert Kent No Comments

Heres a little script that you can use to disect your url and display the parts I’ll show you some uses for this in the new year. But for now I’ll just post it so that you can use your imagination.

I used this in the header.phtml but you can use it anywhere. If anyone can think of a better way of returning categories / subcategories please let me know.

function getCategoryAndSubCategory($page){

if($page == "/"){
$return['category'] = "home";
} else {
$replacements = array("/shop/", ".html");
$page = str_replace($replacements, "", $page);
$pagearr = explode("/", $page);
if(is_array($pagearr)){
$return["category"] = $pagearr[0];
$return["subcategory"] = $pagearr[1];
} else {
$return["category"] = $pagearr[0];
}
}
return $return;

}

$thepage = getCategoryAndSubCategory($thepage);
$category = $thepage["category"];
$subcategory = $thepage["subcategory"];

The above code should give you back your categories and subcategories. It is not perfect but it should help out those who need this functionality. I’ll give you some more data next year when I’ve finished my recursive block script which will sort through folders/subfolders to get the correct phtml file dependant on what category you are in.

Thanks for visiting our Magento Blog here at E-commerce Web Design.

Post A Comment

Your comments:
Enclose code snippets within the appropriate tags: [php][/php]   [js][/js]   [xml][/xml]   [css][/css]   [html][/html]
E.g: [php]<?php echo "hello world"; ?>[/php]

Search Blog

Archives

For the record...

Views & opinions in this blog are those of the individual and do not necessarily reflect those of E-commerce Web Design or the Creare Group.