Conditional Image Thumbnail
Permalink
Hi all,
I'm trying to create a page list template with thumbnails, the issue I'm having is that when a page does not have an image, I get an error:
I know it's caused by those pages that do not have page thumbnails.
I tried following this thread:http://www.concrete5.org/community/forums/customizing_c5/custom-pag...
but I just can't understand what I need to change in my view.php to get this to work. I attached my view.php as well (it's the original page-list view.php with the image attributes added in.
I appreciate all of your help.
I'm trying to create a page list template with thumbnails, the issue I'm having is that when a page does not have an image, I get an error:
Fatal error: Call to a member function getRelativePath() on a non-object in /mysite/blocks/page_list/templates/new-template/view.php on line 27
I know it's caused by those pages that do not have page thumbnails.
I tried following this thread:http://www.concrete5.org/community/forums/customizing_c5/custom-pag...
but I just can't understand what I need to change in my view.php to get this to work. I attached my view.php as well (it's the original page-list view.php with the image attributes added in.
I appreciate all of your help.
why not check it if img exist?
I'm sorry, but I just don't understand what's trying to be accomplished in that code at all. I understand I need an if statement, I just don't know where to put it, as the error line is on line 27.
I tried to put some of that code in my view.php, but wasn't sure how to fix it.
I tried to put some of that code in my view.php, but wasn't sure how to fix it.
change line 27
with this
see if the error still occur //if i not wrong :)
$img_src = $img->getRelativePath();
with this
if(isset($img->getRelativePath())){ $img_src = $img->getRelativePath(); }else{ $img_src = 'some/default/thumbnail/here'; }
see if the error still occur //if i not wrong :)
Thanks for your help fastcrash,
unfortunately, that code gives me an error:
Fatal error: Can't use method return value in write context in view.php on line 27
unfortunately, that code gives me an error:
Fatal error: Can't use method return value in write context in view.php on line 27
how about this
sory, i'm not pro after all , hahaa
if($img){ //just change this $img_src = $img->getRelativePath(); }else{ $img_src = 'some/default/thumbnail/here'; }
sory, i'm not pro after all , hahaa
fastcrash,
Unfortunately, that one gave me this error:
Fatal error: Call to a member function getPath() on a non-object in /site.com/blocks/page_list/templates/events/view.php on line 33
Line 33 now is:
Ideally, I'd like to have just the page title, and no default thumb if there's none for the page.
Thanks again for your help!
Unfortunately, that one gave me this error:
Fatal error: Call to a member function getPath() on a non-object in /site.com/blocks/page_list/templates/events/view.php on line 33
Line 33 now is:
list($img_width, $img_height) = getimagesize($img->getPath());
Ideally, I'd like to have just the page title, and no default thumb if there's none for the page.
Thanks again for your help!
maybe like this
if($img){ //just change this $img_src = $img->getRelativePath(); list($img_width, $img_height) = getimagesize($img->getPath()); }
Simply check if $img is a valid File object:
$img = $page->getAttribute('thumbnail'); if(!is_object($img) || !$img instanceof File){ /* No thumbnail image available */ $event_photo = ''; }else{ $img_src = $img->getRelativePath(); /* Skip using getimagesize() */ $img_width = $img->getAttribute('width'); $img_height = $img->getAttribute('height'); $event_photo = '<img src="' . $img_src . '" width="' . $img_width . '" height="' . $img_height . '" alt="Event photo" />'; }
Thank you so much beebs93! That did it!
Sorry, I missed the closing brace on my copy..lol
Sorry, I missed the closing brace on my copy..lol