Get category URL key in Magento

65,209

Solution 1

Both other answers there is a DB penalty. Best way to add Category URL info is at the collection level and simply use it to your liking in your template files. Adjust your code as follows:

    $_categories = Mage::getModel('catalog/category')->getCollection()
                     ->addAttributeToSelect('name')
                     ->addAttributeToSelect('is_active')
                     ->addUrlRewriteToResult();

<?php foreach($_categories as $_category): ?>
<a href="<?php echo $_category->getUrl($_category); ?>">
  <?php endforeach; ?>

Notice the additional method applied to the Category Collection called addUrlRewriteToResult() and call the url by using the getUrl() instead of what you had before which was getCategoryUrl() (no such thing in the code).

By the way, your code should work just fine if you call getUrl() but will impact performance slightly.

I hope this helps.

Solution 2

Maybe i did not understand the question completely but the code below will give you the url of a category given the id

<?php $category = Mage::getModel('catalog/category')->load(4); ?>
<a href="<?php echo $category->getUrl(); ?>">

Simply change the id 4 inside load() with the one you need

Solution 3

Using Magento (Category-) models might become very heavy to load just for loading the URL of category. When you're in a loop where you need to load the URL's of 9000+ category URL's you might consider using the url rewrite functionality to fetch the URL's since it doesn't involve loading numerous Magento models:

$requestPath = Mage::getSingleton('core/url_rewrite')
    ->getResource()
    ->getRequestPathByIdPath(
        'category/' . $categoryId, Mage::app()->getStore()->getId());
return Mage::getBaseUrl() . $requestPath;

Read this article for more information on this one.

Solution 4

Use Mage::helper('catalog/category') for this

<?php $_helper= Mage::helper('catalog/category');
      $_categories = Mage::getModel('catalog/category')->getCollection()
                     ->addAttributeToSelect('name')
                     ->addAttributeToSelect('is_active'); ?>
<?php foreach($_categories as $_category): ?>
    <a href="<?php echo $_helper->getCategoryUrl($_category); ?>">
          <?php echo $_category->getName(); ?>
    </a>
<?php endforeach;?>

More info to click hear

Share:
65,209
marchemike
Author by

marchemike

programmer

Updated on July 17, 2022

Comments

  • marchemike
    marchemike almost 2 years

    How do I get the URL key of a category in Magento. I have added this text in URL key field the CMS:

    Category-1
    

    This is how I'm currently trying to show my category URL in an anchor:

    $_categories = Mage::getModel('catalog/category')->getCollection()
                         ->addAttributeToSelect('name')
                         ->addAttributeToSelect('is_active');
    
    <?php foreach($_categories as $_category): ?>
    <a href="<?php echo $_category->getCategoryUrl($_category); ?>">
      <?php endforeach; ?>
    

    But whenever I check my output it still shows like this:

    <a href="">
                <span>Manual Tile Cutters</span>
            </a>
    

    I have already checked google and the magento forums for this, but I still cannot find a sufficient answer.

    Also, is what I'm trying to call in the anchor the URL key, or is it a different URL?