1

I'm trying to get a custom attribute >> type "text field" into the header.phtml.

To get a custom attribute into a product page it is (view.phtml)

<?php echo $_product->getAttributeText('medium');?>

What is it for getting it into header.phtml?

2 Answers 2

-1

As header file is called every page ,So i have check current page is product by full action

       <?php
 $FullActionName= Mage::app()->getFrontController()->getAction()->getFullActionName();
        if($FullActionName=='catalog_product_view'):
        if(Mage::registry('current_product')){
        $Product=Mage::registry('current_product');
    ?>

    <meta itemprop="description" content= "<?php echo $Product->getMedium();?>" />
      <?php  //echo $Product->getMedium();
        }
        endif;
?>

According to your request i am putting update code

 <?php
    $FullActionName= Mage::app()->getFrontController()->getAction()->getFullActionName();
    if($FullActionName=='catalog_product_view'){
         if(Mage::registry('current_product')){
             $Product=Mage::registry('current_product');
            if(Mage::registry('my_current_pro')){
            Mage::unregister('my_current_pro');
            }
             Mage::register('my_current_pro',$Product->getId());
             ?>
            <meta itemprop="description" content= "<?php echo $Product->getMedium();?>" />
             <?php   
         }
    }else{
        if(Mage::registry('my_current_pro')){
             $Product=Mage::getModel('catalog/product')->load(Mage::registry('my_current_pro'));
             ?>
             <meta itemprop="description" content= "<?php echo $Product->getMedium();?>" />
             <?php
        }

    }

But ,most import except product page,rest of page will be showing the meta data basic of last visited product.

21
  • why my answer is down voted ?
    – Amit Bera
    Commented Aug 9, 2014 at 9:32
  • This is what i use in view.phtml to get a custom attribute <meta itemprop="description" content= "<?php echo $_product->getAttributeText('medium');?>" /> I now want to get the same custom attribute in the header.phtml Commented Aug 9, 2014 at 9:36
  • you can use this code header.phtml... i have test it
    – Amit Bera
    Commented Aug 9, 2014 at 9:37
  • How do I use it with a meta tag? <meta itemprop="description" content= "<?php echo $_product->getAttributeText('medium');?>" /> What do I need to change? Commented Aug 9, 2014 at 9:49
  • is you using in header.phtml?
    – Amit Bera
    Commented Aug 9, 2014 at 9:56
0

Use Mage::registry('current_product')->getData('medium') instead of your code. Mage::registry('current_product')->getMedium() should work too.

7
  • so what will be the full php code? if the attribute name is "medium" Commented Aug 9, 2014 at 9:23
  • Just changed the answer, check it please.
    – mbalparda
    Commented Aug 9, 2014 at 9:30
  • so this will be what the final code will look like? <?php echo Mage::registry('current_product')->getData('medium');?> Commented Aug 9, 2014 at 9:41
  • Yes. It should work.
    – mbalparda
    Commented Aug 9, 2014 at 9:44
  • neither of the solutions work, it breaks the page. and shows this error <meta itemprop="description" content="<br /> <b>Fatal error</b>: Call to a member function getMedium() on a non-object in Commented Aug 9, 2014 at 9:48

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.