Retrieve Categories / Subcategories
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.
