Skip to content

Instantly share code, notes, and snippets.

@b2z
Created January 29, 2020 12:26
Show Gist options
  • Save b2z/70239a935b6d8a4ffa2d7668f8889098 to your computer and use it in GitHub Desktop.
Save b2z/70239a935b6d8a4ffa2d7668f8889098 to your computer and use it in GitHub Desktop.
Override for mod_menu with the count of articles for each category. Put it to /templates/{your_template}/html/mod_menu/default_component.php. Tested on Joomla 3.
<?php
/**
* @package Joomla.Site
* @subpackage mod_menu
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
use Joomla\CMS\Categories\Categories;
// Initialize our output.
$itemsCount = '';
// If this menu item is for com_content.category, lets count articles in it.
if ($item->query['option']=='com_content'
&& $item->query['view']=='category')
{
// Get Categories instance with the option to count items in it.
$categories = Categories::getInstance('Content', ['countItems' => 1]);
$numItems = $categories->get($item->query['id'])->numitems;
// Style for our output.
if ($numItems > 0)
{
$itemsCount = ' <span class="articles-count">(' . $numItems . ')</span>';
}
}
$attributes = array();
if ($item->anchor_title)
{
$attributes['title'] = $item->anchor_title;
}
if ($item->anchor_css)
{
$attributes['class'] = $item->anchor_css;
}
if ($item->anchor_rel)
{
$attributes['rel'] = $item->anchor_rel;
}
$linktype = $item->title;
if ($item->menu_image)
{
if ($item->menu_image_css)
{
$image_attributes['class'] = $item->menu_image_css;
$linktype = JHtml::_('image', $item->menu_image, $item->title, $image_attributes);
}
else
{
$linktype = JHtml::_('image', $item->menu_image, $item->title);
}
if ($item->params->get('menu_text', 1))
{
$linktype .= '<span class="image-title">' . $item->title . '</span>';
}
}
if ($item->browserNav == 1)
{
$attributes['target'] = '_blank';
}
elseif ($item->browserNav == 2)
{
$options = 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes';
$attributes['onclick'] = "window.open(this.href, 'targetWindow', '" . $options . "'); return false;";
}
// Add articles count to link text.
$linktype .= $itemsCount;
echo JHtml::_('link', JFilterOutput::ampReplace(htmlspecialchars($item->flink, ENT_COMPAT, 'UTF-8', false)), $linktype, $attributes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment