Magento 1.9 – Get Custom Attribute’s Value in Index Page

attributesmagento-1.9

I have custom attribute called slider_link and trying to display it's value in home/index/cms page. I use the following code:

<?php echo $_product->getData("slider_link"); ?>

After adding this, my index page dose not load.

However, I had used above code on my view.phtml and list.phtml with no errors. everything is working fine.

Any idea how can I get the attribute value of my custom attribute called slider_link on my home page?

UPDATED

<div class="flexslider">
    <ul class="slides">
    <?php 
    $products = Mage::getModel('catalog/product')->getCollection();
    $products->joinField(
    'qty', 
    'cataloginventory/stock_item', 
    'qty', 
    'product_id=entity_id', 
    '{{table}}.stock_id=1', 
    'left' 
    );
    $products->addAttributeToSelect(array('name', 'thumbnail', 'price', 'special_price' , 'RegularPrice' )); //ovdje idu atributi
    $products->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id=entity_id', null, 'left' );
    $products->getSelect()->limit(5);
    $products->getSelect()->order('RAND()'); // random sortiranje
    $products->addAttributeToFilter('category_id', array('in' => array(21))); // ubacujem kategorije id
    $this->_reviewsHelperBlock = $this->getLayout()->createBlock('review/helper'); // pozivanje review helper, da bi se prikazao review summary
    foreach ($products as $product)  : 
    ?> 
        <li>
            <a href="<?php echo $_product->getAttributeText("slider_link"); ?>"><img class="thumbnail" src="<?php echo Mage::helper('catalog/image')->init($product, 'thumbnail')->resize(780, 395)?>" alt="<?php echo $product->getName()?>"></a>
        </li>
    <?php endforeach; ?>
    </ul>
</div>

Best Answer

You are calling this

<?php echo $_product->getData("slider_link"); ?>

but the variable $_product does not exist.
you loop looks like this:

foreach ($products as $product)  :

So you should use echo $product->getData("slider_link") with no underscore for the product variable.

Also, you should add the attribute slider_link to the collection.
This

$products->addAttributeToSelect(array('name', 'thumbnail', 'price', 'special_price' , 'RegularPrice' )); //ovdje idu atributi

should be

$products->addAttributeToSelect(array('name', 'thumbnail', 'price', 'special_price' , 'RegularPrice', 'slider_link' )); //ovdje idu atributi
Related Topic