How to get url of a category or product in magento .phtml file

For category url by category ID:

1
 <?php echo Mage::getModel("catalog/category")->load(5)->getUrl() ?>

To load products of a specific category:

1
2
3
4
5
 <?php                      
$cat_id = 8; // set desired category id$category = Mage::getModel('catalog/category')->load($cat_id);$products = $category->getProductCollection()->addCategoryFilter($category)->addAttributeToSelect('*');?>
<?php foreach ( $products as $_product ): ?>
    <a href="<?php echo $_product->getProductUrl() ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 135); ?>" width="135" height="135" title="<?php echo $_product->getName() ?>" alt="<?php echo $_product->getName() ?>" /></a>
<?php endforeach; ?>

Leave a Reply