3

I want to display number of published articles in Joomla menu modules,
like the below image but I can't.

Please help me. Thanks
menu

  • It's work only if article <20, 20 is max – user5422 Jun 12 '15 at 14:45
8

You can achieve this by overriding menu module.

Let say your current template is protostar then create a file

<JOOMLA FOLDER>/templates/protostar/html/mod_menu/default_component.php

and add code like below (not optimized but it's working):

<?php
/**
 * @package     Joomla.Site
 * @subpackage  mod_menu
 *
 * @copyright   Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;
if($item->query['option']=='com_content' && $item->query['view']=='category')
{
    $catId=$item->query['id'];
    $model = JModelLegacy::getInstance('Articles', 'ContentModel');
    $model->setState('filter.category_id', $catId); 
    $articles = $model->getItems();
    $num_articles = count($articles);
    $item->title='<span class="article-count">'.$num_articles.'</span> '.$item->title;
}
// Note. It is important to remove spaces between elements.
$class = $item->anchor_css ? 'class="' . $item->anchor_css . '" ' : '';
$title = $item->anchor_title ? 'title="' . $item->anchor_title . '" ' : '';

if ($item->menu_image)
{
    $item->params->get('menu_text', 1) ?
    $linktype = '<img src="' . $item->menu_image . '" alt="' . $item->title . '" /><span class="image-title">' . $item->title . '</span> ' :
    $linktype = '<img src="' . $item->menu_image . '" alt="' . $item->title . '" />';
}
else
{
    $linktype = $item->title;
}

switch ($item->browserNav)
{
    default:
    case 0:
?><a <?php echo $class; ?>href="<?php echo $item->flink; ?>" <?php echo $title; ?>><?php echo $linktype; ?></a><?php
        break;
    case 1:
        // _blank
?><a <?php echo $class; ?>href="<?php echo $item->flink; ?>" target="_blank" <?php echo $title; ?>><?php echo $linktype; ?></a><?php
        break;
    case 2:
    // Use JavaScript "window.open"
?><a <?php echo $class; ?>href="<?php echo $item->flink; ?>" onclick="window.open(this.href,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes');return false;" <?php echo $title; ?>><?php echo $linktype; ?></a>
<?php
        break;
}
3

A quick Google search for "get number of articles joomla" gave me a similar question from StackOverflow with the following code:

$model = JModelLegacy::getInstance('Articles', 'ContentModel');
$model->setState('filter.category_id', 2); // Set category ID here
$articles = $model->getItems();

$num_articles = count($articles); // Returns the number of articles in category

You should be able to create a PHP loop if you need to count articles in several categories.

Hope that helps.

  • tanks but now the question is how can i get category id in each menu? – hhgh4269 Nov 9 '14 at 7:24
  • if we see the menu table in database we find the link fields that contain the link of each menu and for category type of menu the link has the id of category at the end. – hhgh4269 Nov 9 '14 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.