$category->getPath()
will return the ids of all categories from the tree root to the current one separated by slash (/
). Here is an example: 1/2/56/124/543
.
The first one is the 'root of roots'. The second one is the catalog root (default category). The rest of them are simple categories.
So you can do something like this.
$path = $category->getPath();
$ids = explode('/', $path);
if (isset($ids[2])){
$topParent = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($ids[2]);
}
else{
$topParent = null;//it means you are in one catalog root.
}
Now you can get the name and url like this:
if ($topParent){
$name = $topParent->getName();
$url = $topParent->getUrl();
}
If you have only one language so there is only one way to sort categories alphabetically I recommend changing their position so you will let Magento do its magic.
If you have more than one language, or you need the categories sorted alphabetically only on frontend ignore this answer.
Here is a small script that sets the category position.
$parentId
- it the category id for which we sort the children
$conn
- is a database connection because there is no need to involve the full internal API for changing only one field. we can run queries directly on the DB. I know that this is a big 'NO-NO' but in this case it works.
function orderCategories($parentId, $conn){
//The position field is in the main category table
//add prefix if you have one
$table = 'catalog_category_entity';
//get the children for a specific category id
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('parent_id', $parentId);
$categories = array();
foreach ($collection as $category){
//in case there are 2 categories with the same name we shouldn't skip them
//but there shouldn't be
$categories[$category->getName().'_'.$category->getId()] = $category->getId();
}
//sort categories by name
ksort($categories);
//set their position
$position = 1;
foreach ($categories as $name=>$id){
$q = "UPDATE {$table} SET `position` = {$position} where `entity_id` = {$id}";
$conn->query($q);
$position++;
//sort the current category children by calling the same function recursively
orderCategories($id, $conn);
}
}
Now that we have the function we just need to run it.
For this create a file. Let's call it categories.php
and place it in the root of your Magento instance. The file should contain this:
<?php
//set error reporting to see if something is wrong.
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
//set developer mode for additional debug functionality
Mage::setIsDeveloperMode(true);
//instantiate Mage_Core_Model_App;
Mage::app();
//set the initial parameters for the function
$root = 1;
$conn = Mage::getSingleton('core/resource')->getConnection('core_write');
//sort categories
orderCategories($root, $conn);
You can add the function defined above in the same file below these lines
Now you just need to call in the browser mysite.com/categories.php
.
Clear the cache and reindex the category flat data and you are done.
Just to make sure, backup your database before trying it.
Best Answer
Each category has section called 'Custom Design'. From that section you can select a different theme for a category, add custom layout update statements, and select the layout of the page (1column, 3columns, ...). You can create a different theme containing only the files you need to change for the categories and use that one for your 3 top categories.
Also in the section 'Display settings' you can choose for 'Display mode' 'Static block only' if you don't want products listed (or 'Static blocks and products' if you want a static content and products listed) and create a static block with the content and design you want and use that for your categories. One block for each category.