I. Adding Custom Admin Templates

Following these steps, you can add custom admin templates with ease.
Step 1. In the file administrator/components/com_k2/views/item/view.html.php, you paste the following code just after
$this->assignRef(‘form’, $form); (around line 307)

//Look for specific admin template file  
$componentPath = JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'templates'; 
$componentFolders = JFolder::folders($componentPath); 
$db =& JFactory::getDBO(); 
$query = "SELECT template FROM #__templates_menu WHERE client_id = 0 AND menuid = 0"; 
$defaultemplate = $db->loadResult(); 

if (JFolder::exists(JPATH_SITE.DS.'templates'.DS.$defaultemplate.DS.'html'.DS.'com_k2'.DS.'templates'.DS.'admin'.DS.$item->catid)) 

Step 2: Duplicate the folder and contents of /administrator/components/com_k2/views/item/tmpl, move it to templates/MY_TEMPLATE/html/com_k2/admin and rename the new folder with the category id you want to override. Then you edit the enclosed file default.php (the template for the k2 item backend screen) as much as you want to change the admin appearance for that category.

Click the following link for an example of admin view for editing a specific category with the admin item view override.


II. Calling Specific Extra Field Data on the Frontend

Step 1: Create a new K2 template – copy the folder and contents from components/com_k2/templates/default to templates/[your joomla template]/html/com_k2/templates and rename it.

Step 2: In the item.php file in your new template, you insert the following code just after K2 Plugins: K2BeforeDisplayContent is called.

<!-- Call to prepare extra fields --> 
//convertArray to use ids as key
$extrafields = array();
foreach($this->item->extra_fields as $item)
$extrafields[$item->id] = $item->value;

Note: Ive actually only used this in the item view, but Id imagine it would work fine in the category_item view as well. UPDATE: it works fine there too.

Note 2: I had a different method posted here before but I found out a better way to do it. The old way put the extra field data in an array based on the ordering of the fields but that meant that if you wanted to reorder the fields – something that I couldnt figure out at first but is possible, just for some reason you have to click order at the top of the column before it lets you – anyway, this would change the array and therefore break the template you made based on the specific extra fields. This way, the extrafield values are based on their ids, so you can reorder all you want and they will still work fine.

Step 3: Use the following bit of code anywhere to call any extra field value by its id.

<?php echo $extrafields[13];?>

The array value in the brackets after the variable will correspond to the id of the extra field. you can see these in the backend extra fields view.

An example of how I used this was to make a custom social media link section on a site thats easily updated by the client – just by putting in the links they want for each item in the extra fields of that category, rather than having to try and use the editor. It also checks if there is any value in each before displaying the icon:

<div class="artistSocialMedia">
<!-- Twitter -->  <a href="/" 
target="_blank"><img src="/images/smicons/twitter2.png" alt="Twitter" title="Twitter" /></a>   
<!-- Facebook -->  <a href="/" 
target="_blank"><img src="/images/smicons/fb2.png" alt="Facebook" title="Facebook" /></a>    
<!-- Vimeo -->  <a href="/" 
target="_blank"><img src="/images/smicons/vimeo2.png" alt="Vimeo" title="Vimeo" /></a>   
<!-- Youtube -->  <a href="/" 
target="_blank"><img src="/images/smicons/youtube2.png" alt="Youtube" title="Youtube" /></a>   
<!-- Flickr -->  <a href="/" 
target="_blank"><img src="/images/smicons/flickr2.png" alt="Flickr" title="Flickr" /></a>   
<!-- Instagram -->  <a href="/" 
target="_blank"><img src="/images/smicons/instagram2.png" alt="Instagram" title="Instagram" /></a> <?php endif; ?>

Step 4. Figure out how to make all this into a plugin, but in far from that at the moment…

Thanks for your reading, and if you have any questions with this blog, don’t hesitate to let us know by leaving a comment.

5/5 - (1 vote)
Lt Digital Team (Content &Amp; Marketing)

Summer Sale Grab 50% Off for everything on today, don't miss it. Coupon code: SUMMER2024 Redeem Now
Summer Sale Grab 50% Off for everything on today, don't miss it. Coupon code: SUMMER2024 Redeem Now