0

On our homepage we display a product grid with 6 products. We want to display below every product a link to the category of that product.

So for example one of the products is a laptop. Below that laptop I want to display a link with "See all laptops".

Is this possible?

1 Answer 1

1

You could retrieve the list of urls of all the categories of a product with the following code:

<?php
    $categories = $_product->getCategoryIds();  //$_product: Mage_Catalog_Model_Product

    foreach ($categories AS $category) {
        echo Mage::getModel('catalog/category')->load($category)->getUrl(); //get all the URLs here
    }
?>

UPDATED: (To exclude certain category)

<?php
    $categories = $_product->getCategoryIds();  //$_product: Mage_Catalog_Model_Product

    foreach ($categories AS $category) {
        if ($category != REPLACE_WITH_YOUR_HOME_CAT_ID_HERE) {
            $cat = Mage::getModel('catalog/category')->load($category);
            echo "<a href=\"" . $cat->getUrl() . "\"><i class=\"test\"></i>See all " . $cat->getName() . "</a>";

            //to print only one link
            break;
        }
    }
?>
12
  • Great, thanks! But I want to skip the homepage category and also make it a href. We use this category for home (ourdomain/homepage/homepage-row-2) How can I achieve this?
    – JGeer
    Commented Jan 22, 2015 at 8:09
  • @Jelle Sorry, but what do you mean by "We use this category for home"?
    – d.yuk
    Commented Jan 22, 2015 at 8:51
  • @Jelle And what if the product falls into more than 1 category (excluding the "homepage category")?
    – d.yuk
    Commented Jan 22, 2015 at 8:52
  • @Jelle Oh, do you mean the 6 products are in a "homepage category" which is created to display those 6 products only, and you want to exclude this particular category?
    – d.yuk
    Commented Jan 22, 2015 at 8:54
  • The products falls into just 2 categories. That is the category for the homepage. We call the category for the homepage (homepage-row-2). And the product falls into the category where their belong. So I want to display only the category where their belong and exclude the homepage category
    – JGeer
    Commented Jan 22, 2015 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.