Welcome Joomler!
On this page, you will find some useful tips and snippets for overriding your Joomla layouts. I collect them since years and I've decided to share these helpful piece of code with Jommla overrides friends. Don't hesistate to bookmark this page with CTRL+D
.
Caution!
Before creating and applying an override on your Joomla site, you must backup your site. Do it again. And again.. In any case, I will not be responsible of an error on your website.
Thanks in advance to give me feedback about these overrides. Feel free to share yours and to improve mine.
Some useful and official articles about Joomla layout and output overrides
Some useful videos about Joomla overrides
Some useful php codes for your overrides
-
Print_r
print_r — Prints human-readable information about a variable
<?php /** * @package Joomla.Site * @subpackage mod_articles_archive * * @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ defined('_JEXEC') or die; ?> <?php if (!empty($list)) : ?> <ul class="archive-module<?php echo $moduleclass_sfx; ?> mod-list"> <?php foreach ($list as $item) : ?> <div> <pre> <?php print_r($item);?> </pre> </div> <?php endforeach; ?> </ul> <?php endif; ?>
-
Display the intro image of an article
<?php $article_images = json_decode($item->images); $article_image = ''; $article_image_alt = ''; if(isset($article_images->image_intro) && !empty($article_images->image_intro)) { $article_image = $article_images->image_intro; $article_image_alt = $article_images->image_intro_alt; }?> <a href="/<?php echo $item->link; ?>"> <img class="" src="/<?php echo $article_image; ?>" alt="<?php echo $article_image_alt; ?>" > </a>
-
Display custom fields in an override
<?php $customFields = FieldsHelper::getFields('com_content.article', $this->item, true); foreach ($customFields as $customField){ $customFields[$customField->name] = $customField; }?> <p><?php echo $customFields['name-of-the-custom-field']->value; ?></p>
-
Display the number of articles in a category
<?php $model = JModelLegacy::getInstance('Articles', 'ContentModel', array( 'ignore_request' => true )); $params = JFactory::getApplication()->getParams(); $model->setState('params', $params); $model->setState('filter.category_id', xxxx); // xxxx category ID $num_articles = $model->getTotal(); ?> <p><?php echo $num_articles; ?></p>
-
Truncate the introtext
<?php echo JHTML::_('string.truncate', $item->introtext, 100, false, false) ; ?>
-
Display the name of the site
<?php $config = JFactory::getConfig(); echo 'Site name is ' . $config->get( 'sitename' ); ?>
-
Display images from weblink component
<?php $images = json_decode($item->images); ?> <?php if (isset($images->image_first) and !empty($images->image_first)) : ?> <div class="img-intro pull-<?php echo htmlspecialchars($images->float_first); ?>"> <img src="/<?php echo $images->image_first; ?>" /> </div> <?php endif; ?> <?php if (isset($images->image_second) and !empty($images->image_second)) : ?> <div class="img-thumbnail img-intro pull-<?php echo htmlspecialchars($images->float_second); ?>"> <img src="/<?php echo htmlspecialchars($images->image_second); ?>" /> </div> <?php endif; ?>
-
Display the number of articles
<?php if ($params->get('numitems')) : ?> (<?php echo $item->numitems; ?> articles) <?php endif; ?>
Display the category name
<?php echo $item->displayCategoryTitle; ?>
-
Display the content of URL A field
<?php echo $item->urls->get('urlatext'); ?>
-
Display the image of the category
<img src="<?php echo $item->getParams()->get('image') ?>" class=""/>