Correct code for image attribute

Permalink 1 user found helpful
Hello!
I have problem with code to call the image attribute in page list.
I put a code:
<img width="250px" src=<?php echo($c->getAttribute('okladka')->getVersion()->getRelativePath());?>>

The site works perfect. But when I try to add page by form this error occur:
Call to a member function getVersion() on a non-object.
I'm using Concrete 5.7

View Replies:
MrKDilkington replied on at Permalink Reply
MrKDilkington
Hi tzmudzinski,

Did you try it without getVersion()?
tzmudzinski replied on at Permalink Reply
Yes I did and there is an error:
Call to a member function getRelativePath() on a non-object

I notice that the problem appears when there is no value to the attribute.
MrKDilkington replied on at Permalink Reply
MrKDilkington
Are you including this before you get the attribute?
$c = Page::getCurrentPage();
tzmudzinski replied on at Permalink Reply
Yes i did and have the same error (Call to a member function getRelativePath() on a non-object)

this is the code what I have on my site:

<?php $this->inc('elements/header.php'); ?>
<h1><?php echo $c->getCollectionName() ?></h1>
   <?php $c = Page::getCurrentPage(); ?>
<table cellpadding="10" style="width: 100%" class="tekst">
   <tr>
      <td width="250px" valign="top">
      <img width="250px" src=<?php echo($c->getAttribute('okladka')->getRelativePath());?>>
      </td>
      <td>
      <!--Opis-->
      <?php echo  $c->getAttribute('Opis');?>
      <!--Opis-->
      </td>
   </tr>
</table>
jordif replied on at Permalink Reply
jordif
Hi,

you should make sure this is an object (before you call the getRelativePath function).

Something like:

<?php
if( is_object($c->getAttribute('okladka')) ) {
   <img width="250px" src=<?php echo($c->getAttribute('okladka')->getRelativePath());?>>
}
?>


Regards,

Jordi
tzmudzinski replied on at Permalink Reply
That works perfect!!!
THANK YOU VERY MUCH!:)