Custom pagelist template - if no image is set for a page, a thumbnail displays but is the previous page's thumb
PermalinkI have a custom pagelist template which pulls a thumbnail from a custom attribute - this works great when an image is selected.
However, if a page doesn't have an image chosen, it seems to display the thumbnail from the previous page.
Here's the code which grabs the image from the custom attribute:
Is there a way to not display an image if one isn't set?
Any help would be much appreciated.
Cheers
Ben
I have this call in the first php block at the top of the page:
// load image helper $imageHelper = Loader::helper("image"); // load page attribute 'project_main_image' and apply thumbnail size $thumbnail = $cobj->getAttribute('main_image'); if ($thumbnail) { $thumbnailObj = $imageHelper->getThumbnail($thumbnail, 240, 155); }
Then I thought I'd try to use a conditional when outputting the image in the markup:
But because I've copied and pasted form other examples, I think my syntax is messed up.
BTW, is a conditional the right way to handle the duplicate image issue?
Cheers
Ben
You're pretty much there, but it just needs a bit tidying up.
You definitely want some sort of conditional logic instead of assuming anything. I even go so far as to check that even if the page attribute is set, that it's the right kind of data. There's nothing stopping you/someone else from changing the page attribute's type from a file to ordinary plain text. If that were to happen you could potentially generate a fatal PHP error that could be displayed to the general public, or, if your site is in production mode, screw things up without you knowing why.
Also, skip using the PHP block - just put this directly in your template file to keep everything together.
<?php $objThumb = NULL; // Get the file object of the image assigned to your page attribute (if any) $objFile = $cobj->getAttribute('image_attr_handle_here'); // If it's a valid file object... if(is_object($objFile) && $objFile instanceof File && !$objFile->error){ // ...load the image helper and generate its thumbnail $ih = Loader::helper('image'); $objThumb = $ih->getThumbnail($objFile, 123, 456); } // If we successfully got a valid thumbnail we write out an HTML <img> tag with the appropriate attributes if(!is_null($objThumb)){ $strHtml = '<img src="' . $objThumb->src . '" width="' . $objThumb->width . '" height="' . $objThumb->height . '" alt="Alt text here" />'; }else{ // Handle your fallback here (eg. Text, a hardcoded default image, etc)
i.e. I'd like this markup to appear only if an image is set - otherwise nothing:
Hope that makes sense - each time I try to echo the markup into the if statement itself, I get syntax errors.
Cheers
Ben
$strHtml = '<span>Fallback HTML here</span>';
...to this:
$strHtml = '';
I tried your suggestion but because the final markup is rendered outside the if statement (at the bottom of beebs93's code), the output is rendered even if no image is set - it just renders it unparsed like this:
I think I need to move the above markup into the if conditional. Then leave the else one blank which would render nothing when no image is set - is that right?
The problem is that I run into syntax errors when I try moving the markup into the if statement. I tried removing the extra <?php calls but realised I have no idea what I'm doing.
Sorry if I'm missing something simple.
(Note that the forum software prevents attaching files that end in .php, so either ZIP it up or change the extension to .txt)
The page_list template is attached. The code which renders the markup is located further down on line 65.
Let me know if I'm getting warmer...
P.S. On line 61, change:
to
as you've got your closing tags backwards.
I don't want to skip the page, I just don't want the image markup appearing so the excerpt text slides over.
You can see what's happening on this page:http://dev.sciasciabrothers.co.nz/steelpipe/index.php/projects/...
As you scroll down, you'll notice some entries don't have images but there's a hole for one. That's because the markup is getting rendered even when no image is set.
What I would like is for the markup to not render if no image is set - so that the text slides over. I'm sure I've done this on a c5 site but I can't find the markup.
From memory, I moved the if statement to where the markup is being rendered so that it contains the <div class="unit unit size2of5"> markup.
But each time I do, I get syntax errors.
Hope this makes sense!
<?php //IMAGE THUMBS $objThumb = NULL; // Get the file object of the image assigned to your page attribute (if any) $objFile = $cobj->getAttribute('main_image'); // If it's a valid file object... if(is_object($objFile) && $objFile instanceof File && !$objFile->error){ // ...load the image helper and generate its thumbnail $ih = Loader::helper('image'); $objThumb = $ih->getThumbnail($objFile, 240, 155); } ?> <div class="group project"> <h4><a <?php echo $target ?> href="<?php echo $link ?>"><?php echo $title ?></h4></a> <!-- entry thumbnail -->
But neither example actually renders the thumbnail - I see the div and link markup but no image.
Is this the bit that's supposed to render the image?
<?php echo $strHtml; ?>
If I could mark both answers as best I would - in the end I understood beebs93 answer more so have chosen his.
Thanks heaps for both your answers - very much appreciated.
Cheers
Ben
(Also he put in more effort here explaining it -- nice work!)
Glad you were able to solve the problem.
-Jordan
Plus, I agree with Jordan; he has enough karma to ensure he'll be at the cool table in the afterlife (the one where Johnny Cash, John Wayne and Johnny Carson sit) ;)
I'm not sure on your setup, but you may want to modify this so if the page attribute isn't set that the entire link is not shown, but that's up to you.
Hope this helps :)