9

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?

marchemike
  • 3,179
  • 13
  • 53
  • 96

5 Answers5

21

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.

Ron
  • 798
  • 4
  • 13
  • Unfortunately, every getUrl($_category) call still leads to an additional DB query so I disagree that this is very efficient. – satnam Sep 07 '17 at 02:13
  • Not if it is prefetched in the category collection ahead of time. The getUrl() will fetch the data from the collection object and would not require an additional DB query. Try it. – Ron Sep 08 '17 at 16:33
  • Yes I tried it. My mysql logs shows that additional queries were made for each getUrl() call – satnam Sep 08 '17 at 16:35
  • Did het above in Magento 1.9.3.10 $category->getUrl() still leads to the full URL instead of the domain .... $this->getCategoryUrl($category) does the same .... and $category->getUrlKey() returns NULL – snh_nl Nov 21 '18 at 08:21
17

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

ThanosDi
  • 339
  • 3
  • 8
6

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.

Giel Berkers
  • 2,852
  • 3
  • 39
  • 58
3

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

Ravi Patel
  • 5,121
  • 2
  • 25
  • 44
0

If you want to retrieve a collection of store wise category attributes and category url , you can use

$collection = Mage::getModel('catalog/category')->getCollection()
    ->setStoreId($store->getId())
    ->addAttributeToSelect('entity_id')
    ->addAttributeToSelect('url_key')
    ->addAttributeToSelect('name')
    ->addAttributeToFilter('is_active', 1)
    ->addAttributeToFilter('include_in_menu', 1)
    ->addFieldToFilter('path', array('like'=> "1/{$rootCategoryId}/%"));

    $collection->getSelect()->joinLeft(
    array('url_rewrite_table' => $collection->getTable('core/url_rewrite')),
    "url_rewrite_table.store_id = {$store->getId()} AND id_path = CONCAT('category/',e.entity_id)",
    array('store_url_key' => 'request_path'
    )
    );

And retrieve request path like $row->getStoreUrlKey() and prefix it with the store base Url. I am using this to show store specific category grid in the admin panel.

Anil
  • 1
  • 3