Thursday, 28 February 2019 06:13

Get the URL of a K2 extra field image

K2 extra fields can be a royal pain sometimes. They are not nearly as flexible as they should be. For instance, using the image extra field spits renders the image inside it's <img src /> tags. But what if you just want the URL of the image? Perhaps you need the image URL to use inside a CSS background image. Here's how:

Note that the alias of the extra field in my example is myImage. Change this to match your own custom field alias.
Also note that I am using this code inside a mod_k2_content module.


<?php
$fetchSrc1 = explode('src="', $item->extraFields->myImage->value);
$fetchSrc2 = explode('"', $fetchSrc1[1]);
?>

<?php echo $fetchSrc2[0]; ?>

For using on K2 item pages, replace the first line with the following:


$fetchSrc1
= explode('src="', $this->item->extraFields->myImage->value);

Published in Blog

Sometimes you need to know the category of the current K2 item, but reneder this information outside of the K2 template files. To check anything K2 outside of the K2 container, the code below is a good starting point.
By expanding this code a bit, you can check for all sorts of things, always from outside of the K2 container. For instance, I can use the following inside the <head> tags to trigger something specific to an item.

    
<?php
// Check if the item belongs to a specific category

// Gets component, view, and ID from the non-SEF URL
$whatComp = JFactory::getApplication()->input->get('option');
$whatView = JFactory::getApplication()->input->get('view');
$whatId = JFactory::getApplication()->input->get('id');
// Check if you are on K2 and viewing an item
if($whatComp == 'com_k2' && $whatView == "item") {
// Check the database for the category ID of the current K2 item
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('catid');
$query->from($db->quoteName('#__k2_items'));
$query->where($db->quoteName('id')." = ".$db->quote($whatId));
$db->setQuery($query);
$myCatID = $db->loadResult();
// The number to be compared below is the ID of the category

if($myCatID == '17') {
// do something
}
}
?>

 

Published in Blog

<?php
$parent = JTable::getInstance('K2Category', 'Table');
$parent->load($this->category->parent);
echo $parent->name;
?>

Published in Blog

This is a super handy cheat sheet compiled by the K2 team that solves many PHP conditional statements:

https://github.com/kricore/Advanced-templating-with-K2/blob/master/_inc/cheatsheet.php#L28-L55

Published in Blog

Here's a very handy snippet to create a PHP conditional if statement based on a specific K2 item ID:


<?php
$itemid = JRequest::getInt('Itemid');

if( $itemid == 123 ) {
echo 'do something';
} else {
echo 'these are not the droids you are looking for';
}
?>


<?php
$itemid = JRequest::getInt('Itemid');

if( $itemid == 123 ) {
echo 'do something';
} else {
echo 'these are not the droids you are looking for';
}
?>

The code above can be altered using the following hooks from the page URL:


$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
$layout = JRequest::getCmd('layout');
$page = JRequest::getCmd('page');
$task = JRequest::getCmd('task');
$id = JRequest::getInt('id');

Published in Blog
Monday, 19 October 2015 12:15

Text limit in Joomla

For com_content use the following:

<?php $truncated_text = JHtml::_('string.truncate', $this->item->introtext, 20); ?>
<?php echo $this->item->event->beforeDisplayContent; ?> <?php echo $truncated_text; ?>

For K2 use the following:

<?php echo JHtml::_('string.truncate', $this->item->introtext, 200); ?>
Published in Blog
Sunday, 23 August 2015 04:16

Output specific K2 extra fields

Outputing specific K2 extra fields is dependent on what php document you intend to display the extra fields in. If you're using item.php, or the category-item.php files, the following should work. In the top of the document, add this code:

<!-- Call to prepare extra fields -->
<?php
$extrafields = array();
foreach($this->item->extra_fields as $item)
{
$extrafields[$item->id] = $item->value;
}
?>
Then, wherever you'd like to call the value of the filled in field, use this:

<?php if($extrafields[ID_NUMBER_OF_FIELD]!=''):?> <!-- if filled in, then call data -->
<?php echo $extrafields[ID_NUMBER_OF_FIELD];?> <!-- actual data call -->
<?php endif; ?>
tag.php works a little differently, as do the K2 Modules.

for tag.php you don't need the extra code in the head, the following will call your field data:

<?php $extrafields = json_decode($item->extra_fields);?>
<?php foreach($extrafields as $key=>$value): ?>
<?php if($extrafields[$key]->id == 'ID_NUMBER_OF_FIELD'&&$extrafields[$key]->value!=''): ?>
<?php echo $extrafields[$key]->value; ?>,
<?php endif; ?>
<?php endforeach; ?>
Again, with the module_k2_content, you don't need the top data call. Get your field data this way:

<?php echo $item->extraFields->ALIAS_OF_FIELD->value ;?>

 

For menu links that use the K2 Latest Items layout, call the extra fields within the template override using the following:


<?php $extrafields = json_decode($this->item->extra_fields);?>
<?php foreach($extrafields as $key=>$value): ?>
<?php if($extrafields[$key]->id == '1'&&$extrafields[$key]->value!=''): ?>
<?php echo $extrafields[$key]->value; ?>
<?php endif; ?>
<?php endforeach; ?>

Published in Blog

I knocked up this video explaining how the "read more" divider in k2 works, and how it can effect content that contains elements such as lists within the intro text.

Published in Blog