Jump to content

Recommended Posts

Posted

there is a optional image field of a template.

If I put this code in a template, for page without uploaded an image , it will throw a php error of

"Call to a member function size() on a non-object"

$header_image = $page->img->size(800, 250)->url;

Posted

Why dont you check the image with an if statement?

if($page->img){
$header_image = $page->img->size(800,250)->URL;
}

Or am I misunderstand your question?

Posted

Why dont you check the image with an if statement?

if($page->img){
$header_image = $page->img->size(800,250)->URL;
}

Or am I misunderstand your question?

I tried to use isset for checking, but it's not working.

No more php error for image field without an image, however it returned no value even though there is an image in the field

Posted

I suggest you carefully study and bookmark/reference this table. It is an invaluable reference that will teach/remind you when to use/not use isset, empty, is_null, etc..

PHP type comparison tables

http://php.net/manual/en/types.comparisons.php

As for you image, I think I already answered you question but I'll repeat it here:

If it is a single image field  [an image field with a maximum files allowed of 1]

if($page-img) {}

if it is a multiple image field  [an image filed with maximum files allowed of 0 or > 1, even if it contains only one image in your page]

if(count($page->img)){}
  • Like 2
Posted

When you have a field that is returning an array, like an images field that allows more than 1 file, you want this:

if(count($page->img)) {
  • Like 1
  • 1 month later...
Posted

Is there any performance difference between these?

if(count($page->img)){}
if($page->img->count()){}

Lastly, is it safe to use "count" only and not "count()"? 

if($page->img->count){}
Posted

Thanks! So I should better use count($page->img) as it skips one - minor - step?

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...