The task is to display a video instead of a preview of the K2 material in the general output of materials in the category.
here wrote about how to display a video from YouTube instead of a picture of the K2 material inside the material.
To display images in categories, use the code:
<?php if($this->item->params->get('catItemImage') && !empty($this->item->image)): ?> <!-- Item Image --> <a href="<?php echo $this->item->link; ?>" title="<?php if(!empty($this->item->image_caption)) echo K2HelperUtilities::cleanHtml($this->item->image_caption); else echo K2HelperUtilities::cleanHtml($this->item->title); ?>" style="background-image: url(<?php echo $this->item->image; ?>);" class="item-image-link"> </a> <?php endif; ?>
Following the example Display video instead of preview in K2 Joomla material, the videolink field has already been created and filled with a link from YouTube.
Then edit the category_item.php file and edit the above code like this:
<?php if(isset($this->item->extraFields->videolink->value) === true && empty($this->item->extraFields->videolink->value) === false): ?> <!--<span class="itemExtraFieldsLabel"><?php echo $this->item->extraFields->videolink->name; ?></span>--> <div class="itemExtraFieldsValue"> <iframe width="350" height="197" src="<?php echo $this->item->extraFields->videolink->value; ?>" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> </div> <?php else: ?> /*here is the code with the output of the picture, published at the beginning of the post*/ <?php endif; ?>