Show K2 Attachments

Need to show K2 attachments just to your loyal Joomla members? Here’s how you do it:

Find this code within your relevant K2 template file (item.php, category_item.php):

<?php if($this->item->params->get('catItemAttachments') && count($this->item->attachments)): ?>

Then replace it with this:

<?php if($this->item->params->get('catItemAttachments') && count($this->item->attachments) && !$this->user->guest): ?>

This just checks to make sure the user is not a guest. If they are logged in then the code will validate as true and K2 will proceed with outputting the attachments.

That’s it, all done.

If you also want to provide some text for guests instead of giving them nothing, then you can improve on this code by adding an ‘else’ statement.

So your final code will be:

<?php if($this->item->params->get('catItemAttachments') && count($this->item->attachments) && !$this->user->guest): ?>
 <!-- Item attachments -->
 <div class="catItemAttachmentsBlock">
 <span><?php echo JText::_("Download attachments:"); ?></span>
 <ul class="catItemAttachments">
 <?php foreach ($this->item->attachments as $attachment): ?>
 <a title="<?php echo htmlentities($attachment->titleAttribute, ENT_QUOTES, 'UTF-8'); ?>" href="<?php echo JRoute::_('index.php?option=com_k2&view=item&task=download&id='.$attachment->id); ?>">
 <?php echo $attachment->title ; ?>
 <?php if($this->item->params->get('catItemAttachmentsCounter')): ?>
 <span>(<?php echo $attachment->hits; ?> <?php echo (count($attachment->hits)==1) ? JText::_("download") : JText::_("downloads"); ?>)</span>
 <?php endif; ?>
 <?php endforeach; ?>
 <?php else: ?>
 <div>Login or sign up to view this attachment.</div>
 <?php endif; ?>