By Mist on Wednesday, 02 July 2014
Replies 1
Likes 0
Views 1.1K
Votes 0
Sometimes, you need to output first attachment image of a discussion in your frontpage.post.php template file.

This how to do it.
First, we are getting the first post attachment

<?php
$attachments = $post->getAttachments();
$attachment = isset( $attachments[ 0 ] ) ? $attachments[ 0 ] : false;
?>



Then we output it inside an image placeholder like this

  <?php if ( $attachment ) {?>
<img src="<?php echo JURI::root() . 'index.php?option=com_easydiscuss&controller=attachment&task=displayFile&tmpl=component&size=thumb&id=' . $attachment->id;?>">
<?php } else { ?>
<img src="no_image.jpg">
<?php } ?>


This last code basically check if we have a first attachment to display. If yes, it will display that first attachment, if not it will display a custom "no_image" file

Now, i think this code needs a little bit of tune-up regarding performance.
I was testing it and it seems that the first attachment is not pulled fast.

I think that the code gets ALL attachments and then choose the first one. This could be a problem if we have a lot of attachments inside the post.

So far it works. Maybe some of the team member can confirm that we hit performance problem because we are getting all attachments and then "sort out" the first one instead of getting ONLY the first one.

Whenever you have some spare time guys ...
Hello Mist,

Thanks for sharing and for the heads up. This might be useful to others. We appreciate it.
·
Wednesday, 02 July 2014 09:35
·
0 Likes
·
0 Votes
·
0 Comments
·
View Full Post