andy jones
andy jones

Reputation: 922

Linking to category magento

I'm looking to get a product on a home page slider in magento to link to the category it is in... so far i have:

<?php
     $allIds = $product->getCategoryIds();
     foreach($allIds as $categoryId) {
          $category = Mage::getModel('catalog/category')->load($categoryId);
?>
     <a href="<?php echo $category->getCategoryUrl() ?>"><?php echo$category->getName() ?></a><br/>
<?php
     }
?>

(this runs within a foreach item) This provides me with the categories (which is great), however the:

<?php echo $category->getCategoryUrl() ?>

Does not seem to link to the correct place (it actually doesn't give me anything). Can anyone help on the matter?

Upvotes: 1

Views: 1536

Answers (3)

ScoRpion
ScoRpion

Reputation: 11474

You will find everything related to displaying categories and subcategories here Display Categories and SubCategories in Magento. Hope this will be helpfull..

Upvotes: 0

Slayer Birden
Slayer Birden

Reputation: 3694

if you want to show only one category link, you don't need to load categories in the loop:

$category = $product->getCategory();
$url = $category->getUrl();

Update: I just realized that the 1st line may not work on the homepage. But you still do not need the loop:

$category = $product->getCategoryCollection()->getFirstItem();

Upvotes: 1

Anton S
Anton S

Reputation: 12750

try this

<?php echo Mage::helper('catalog/category')->getCategoryUrl($category);?>

Upvotes: 0

Related Questions