Hide blog thumbnail block if no image
PermalinkI'm using the premade blog_index_thumbnail.php template as my news page. This uses a block to show the thumbnail image, which is added via the composer. I have very little PHP knowledge and have no clue how using this block differs from attributes and which is better, but as it's there I'm using it.
The code looks like this:
<div class="image-link"> <a <?php if ($target != '') { ?> target="<?php echo $target?>" <?php } ?> href="<?php echo $nh->getLinkToCollection($cobj)?>"> <?php $ts = $cobj->getBlocks('Thumbnail Image'); if (is_object($ts[0])) { $tsb = $ts[0]->getInstance(); $thumb = $tsb->getFileObject(); if($thumb){ $imgHelper->outputThumbnail($thumb, 800, 400, $title); } } ?></a> </div><!-- end image-link -->
My issue is, when there is no image added in the composer, that code still tries to show an image that isn't there, so I'm getting a broken image/border. I need an if statement around the whole thing to hide if there is no image added. Can anyone help?

there is example
<?php $ih = Loader::helper('image'); //from your code try this one $thumb = $tsb->getFileObject(); if(is_object($thumb)){ $imgThumb = $ih->getThumbnail($thumb, 800, 400, $title); } ?> <?php if(isset($imgThumb) && is_object($imgThumb)):?> <img src="<?php echo $imgThumb->src?>" alt="<?php echo $title?>"> <?php endif;?>
So you only print the image if image exist
I tried your code but I just get an error. There is already code at the start of the file to load the image helper, so I changed your code to match accordingly:
<?php defined('C5_EXECUTE') or die("Access Denied."); $textHelper = Loader::helper("text"); $imgHelper = Loader::Helper('image'); $dateHelper = Loader::Helper('date'); $isFirst = true; $thumb = $tsb->getFileObject(); if(is_object($thumb)){ $imgThumb = $imgHelper->getThumbnail($thumb, 800, 400, $title); } </php>
However I am getting this error:
Fatal error: Call to a member function getFileObject() on a non-object in /home/mastercraft/public_html/concrete/blocks/page_list/templates/blog_index_thumbnail.php on line 9
This is line 9:
$thumb = $tsb->getFileObject();
The rest of the page is blank. Any ideas what I'm doing wrong?
<div class="image-link"> <a <?php if ($target != '') { ?> target="<?php echo $target?>" <?php } ?> href="<?php echo $nh->getLinkToCollection($cobj)?>"> <?php $ih = Loader::helper('image'); $ts = $cobj->getBlocks('Thumbnail Image'); if (is_object($ts[0])) { $tsb = $ts[0]->getInstance(); $thumb = $tsb->getFileObject(); if(is_object($thumb)){ $imgThumb = $ih->getThumbnail($thumb, 800, 400, false); echo is_object($imgThumb) ? "<img src='{$imgThumb->src}' alt=''/>" : ""; } } ?></a> </div><!-- end image-link -->
if still not work, upload your complete code here.. I'll try to help
I have tried your new code and various different iterations of it but with no success, so here's the complete code for that page:
<?php defined('C5_EXECUTE') or die("Access Denied."); $textHelper = Loader::helper("text"); $imgHelper = Loader::Helper('image'); $dateHelper = Loader::Helper('date'); $isFirst = true; /* @var $dateHelper DateHeler */ // now that we're in the specialized content file for this block type, // we'll include this block type's class, and pass the block to it, and get // the content if (count($cArray) > 0) { ?> <?php for ($i = 0; $i < count($cArray); $i++ ) { $cobj = $cArray[$i]; $target = $cobj->getAttribute('nav_target');
$('img.ccm-output-thumbnail').each(function() { if($(this).attr("src") == "") { $(this).parent().parent().hide(); } });