1 (edited by murko 23-09-2015 10:58:38)

Topic: Number of posts in category?

Any simple way of adding the total amount of posts in all categories in the list in the sidebar?
What I'm looking for is the "(NN)" part if that wasn't clear smile
Example:

Categories
Personal Diary (17)
Work (11)
Fun Stuff (4)
Kittens (0)
Beer (62)


This is the original plugin.bit

<?php
// =====================================================================
// PLUGIN INFO
// =====================================================================
$_PLUGIN_CONFIG['DATA'] = array(
    'author'=>'Diego Najar',
    'version'=>'3.6',
    'url'=>'http://www.nibbleblog.com'
);

// =====================================================================
// PLUGIN CLASS
// =====================================================================
class PLUGIN_CATEGORIES extends Plugin
{
    public function blog_body()
    {
        global $categories;

        $html = '<ul>';

        foreach($categories as $category)
        {
            // URL generator
            $href = Url::category($category['slug']);

            $html .= '<li class="category"><a href="'.$href.'">'.$category['name'].'</a></li>';
        }

        $html .= '</ul>';

        return $html;
    }
}

?>

I also have a suggestion about an archive that I posted in an already started thread: http://forum.nibbleblog.com/viewtopic.p … 3726#p3726

I'm linking to that here aswell, since I think it would make NB even better.
Thanks again for Nibbleblog. I never leave home without it smile

2 (edited by Schlumpfuk 24-09-2015 18:40:59)

Re: Number of posts in category?

I use the following method on my website .... maybe a better way but here goes ...

Add the following after the global declaration, so it looks something like:

        global $categories;

        // initialise postcount variable (used to count number posts per category)
        $postcount = NULL;

Replace the following code:

 
            // URL generator
            $href = Url::category($category['slug']);

            $html .= '<li class="category"><a href="'.$href.'">'.$category['name'].'</a></li>';

With this:

           // URL generator
            $href = Url::category($category['slug']);

           // count number posts per category
           $postcount = count(Filesystem::ls(PATH_POSTS, '*.*.'.$category['id'].'.*.*.*.*.*.*.*.*', 'xml', false, false, false));

           // include post count in category listing
           $html .= '<li><a href="'.$href.'">'.$category['name'].' ('. $postcount .')</a></li>';

This is the only method I found to achieve this issue.

It works for me, but if anyone else has a better solution I'll happily adapt my method :-)

Re: Number of posts in category?

Wow that's great Schlumpfuk smile
Thanks a million.

Just tested on a "blog-in-the-build"
http://easycaptures.com/fs/uploaded/968/7565312696.png

Looks great!


Maybe a stupid question but what exactly does the: $postcount = count(Filesystem::ls(PATH_POSTS, '*.*.'.$category['id'].'.*.*.*.*.*.*.*.*', 'xml', false, false, false)); -part do? Well, count obviously smile
But more spedific the later part maybe:  " '*.*.'.$category['id'].'.*.*.*.*.*.*.*.*', 'xml', false, false, false)); "

4 (edited by Schlumpfuk 25-09-2015 18:26:14)

Re: Number of posts in category?

Hi murko,

I am glad that it answered your query ...

I borrowed that piece of code from Diego's original source ... If you want an understanding of what the function does, take a look in the file admin//kernel/helpers/filesystem.class.php.  Here is the description at the top of the function:

        // Devuelve un arreglo con el listado de archivos
        // $path con una barra al final, ej: /home/
        // $file_expression : *.0.*.*.*.*.*.*.*.*
        // $ext : xml
        // $flag_dir : si quiero listar directorios
        // $sort_asc_numeric : ordeno ascedente numerico
        // $sort_desc_numeric : ordeno descendente numerico

I adapted the categories plugin one step further allowing me to have the categories displayed as a list or pull down menu and/or whether to show the category count ...

Modified Nibbleblog Category Plugin

I think I might just make this public as it is stable for my website.

Re: Number of posts in category?

I think you should smile
Thanks again!

Re: Number of posts in category?

Schlumpfuk wrote:

Hi murko,

I am glad that it answered your query ...

I borrowed that piece of code from Diego's original source ... If you want an understanding of what the function does, take a look in the file admin//kernel/helpers/filesystem.class.php.  Here is the description at the top of the function:

        // Devuelve un arreglo con el listado de archivos
        // $path con una barra al final, ej: /home/
        // $file_expression : *.0.*.*.*.*.*.*.*.*
        // $ext : xml
        // $flag_dir : si quiero listar directorios
        // $sort_asc_numeric : ordeno ascedente numerico
        // $sort_desc_numeric : ordeno descendente numerico

I adapted the categories plugin one step further allowing me to have the categories displayed as a list or pull down menu and/or whether to show the category count ...

Modified Nibbleblog Category Plugin

I think I might just make this public as it is stable for my website.

Please make this public! And thanks for that category posts counter but can counts only published posts?

7 (edited by Schlumpfuk 02-11-2015 18:56:00)

Re: Number of posts in category?

Please make this public! And thanks for that category posts counter but can counts only published posts?


As a published post has the word NULL in the filename at field #5, I guess you could do something like...

$postcount = count(Filesystem::ls(PATH_POSTS, '*.*.'.$category['id'].'.*.NULL.*.*.*.*.*.*', 'xml', false, false, false));

I will get around to making the rest of the changes to the category plugin public this weekend when I get back home.

Re: Number of posts in category?

And how is possible if i have not publiced but only a preview it count it too how to make stop counting non publiced topics?

Re: Number of posts in category?

Bazim wrote:

And how is possible if i have not publiced but only a preview it count it too how to make stop counting non publiced topics?

The answer to that is in the post above yours! smile

Re: Number of posts in category?

Thank you! You are amazing! It work well.