opalepatrick Posted May 5, 2016 Share Posted May 5, 2016 Anyone have any idea why I am getting a call to a non-object width(), size() on this image: foreach ($pages->get("template=case-studies")->children() as $child) { $logo = $child->logo->width(250); echo "<img class='floatleft' src='" . $logo->url . "' />"; echo "<h2>" . $child->title . "</h2>"; echo "<p>" . $child->body . "</p>"; } the logo field is an image field for one image only. If I remove the width or size parameter all works fine. Surely $child->logo is still referring to an object? Or am I misunderstanding things? Link to comment Share on other sites More sharing options...
matjazp Posted May 5, 2016 Share Posted May 5, 2016 I would check the settings for logo Imagefield ... try $child->logo->first()->width() Link to comment Share on other sites More sharing options...
Macrura Posted May 5, 2016 Share Posted May 5, 2016 also to be safe you should make sure that there is a logo.. foreach ($pages->get("template=case-studies")->children("logo!=") as $child) { 2 Link to comment Share on other sites More sharing options...
Soma Posted May 5, 2016 Share Posted May 5, 2016 As with most code in this forum this is not real and best practice code as macrura also points out. Once there's no image it will fail. Also body I asume is a RTE field and will have a <p> already. 2 Link to comment Share on other sites More sharing options...
opalepatrick Posted May 5, 2016 Author Share Posted May 5, 2016 Thank you for all your replies. I had checked for the image and its settings. So as I stated in my post, it was for a single image, but I should have been more explicit instead of just saying that when I removed width() everything was 'fine'. What I should have said was that the image appeared in original unchanged form. This was why I was baffled because the image appeared unless the width() or size() parameter was added to the code. However... when I added macrura's check, the width() parameter worked. I removed the check and it still worked and no problem since. I know something must have changed but I have not got a clue why. Suffice to say that everything now works as I would expect. So rather than waste everyone's time any more, I consider it solved Once again. Thank you. PS, You are right Soma the body field is in fact Markup and should not be wrapped in <p> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now